Quantcast
Channel: Planet Python
Viewing all 22647 articles
Browse latest View live

Rene Dudfield: Drawing data. With #flask, and #matplotlib.

$
0
0
Here's a pretty simple example of using the Flask microweb framework, and matplotlib together.
Matplotlib is a graphing library used by a lot of people in python.

But how to serve your graphs up in a web browser as a png or an SVG image?
Code is in this gist: flask matplotlib.

Serving svg and png images with Flask and matplotlib.


flask and matplotlib for drawing svg and png graphs with python
here is what it looks like

Code is in this gist: flask matplotlib.

Catalin George Festila: Python Qt5 - QFileDialog and QTextEdit example.

$
0
0
This tutorial is about QFileDialog and how to use it.
First you need to create a default PyQ5 application and add your method is called by my_OpenDialog.
This will be connected to one button.
My application uses the QTextEdit to show HTML and text files.
If you try to see another file this will open badly.
I set the completeSuffix just for HTML and text.
As you know this returns the complete suffix (extension) of the file.
The content of this file is put into QTextEdit widget create and named editor_text.
This is result of using QFileDialog with python:

This is the source code with the QFileDialog example:
import sys, os
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setWindowTitle('Text document')
self.editor_text = QtWidgets.QTextEdit(self)

self.button_OpenDialog = QtWidgets.QPushButton('Open', self)
self.button_OpenDialog.clicked.connect(self.my_OpenDialog)

layout = QtWidgets.QGridLayout(self)
layout.addWidget(self.editor_text, 0, 0, 1, 1)
layout.addWidget(self.button_OpenDialog, 1, 0)
#self.handleTextChanged()

def my_OpenDialog(self):
path = QtWidgets.QFileDialog.getOpenFileName(
self, 'Open file', '',
'HTML files (*.html);;Text files (*.txt)')[0]
if path:
file = QtCore.QFile(path)
if file.open(QtCore.QIODevice.ReadOnly):
stream = QtCore.QTextStream(file)
text = stream.readAll()
info = QtCore.QFileInfo(path)
if info.completeSuffix() == 'html':
self.editor_text.setHtml(text)
else:
self.editor_text.setPlainText(text)
file.close()

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())

Import Python: Issue 188 - Python This week

$
0
0
Worthy Read

Want to deliver faster? Want to recover from failures more quickly? Want to improve your cycle time? This Actionable Continuous Delivery Metrics eBook will provide you insight into your software delivery pipeline, and help you to improve your delivery and team processes.
ebook
,
sponsor
,
devops

In almost every project we work on, we use Django admin for support and operations. Over time we experienced an influx of new users and the amount of data we had stored grew rapidly. With a large dataset we started to experience the real cost of some Django admin features.
admin

In this paper, we will talk about the basic steps of text preprocessing. These steps are needed for transferring text from human language to machine-readable format for further processing. We will also discuss text preprocessing tools.
text preprocessing

One of the greatest challenges associated with maintaining a complex desktop application like Dropbox is that with hundreds of millions of installs, even the smallest bugs can end up affecting a very large number of users. Bugs inevitably will strike, and while most of them allow the application to recover, some cause the application to terminate. These terminations, or “crashes,” are highly disruptive events: when Dropbox stops, synchronization stops. To ensure uninterrupted sync for our users we automatically detect and report all crashes and take steps to restart our application when they occur.
python
,
desktop

If you’re planning to learn data analysis, machine learning, or data science tools in python, you’re most likely going to be using the wonderful pandas library.
panfas

Algojammer is an experimental, proof-of-concept code editor for writing algorithms in Python. It was mainly written to assist with solving the kind of algorithm problems that feature in competitions like Google Code Jam, Topcoder and HackerRank.
editor

In the first episode, we got to the bottom of the error reported by mypy: we understood exactly what was wrong with the initial code and why it wasn’t type-safe. Now, we need to do something about it. The goal of this episode is not to give the ultimate solution to the problem, but to approach it from different perspectives and provide some (fairly simple) suggestions. Choosing and implementing the right one depends on the specific use case.
mypy

Vespene is a modern, streamlined build and self-service automation platform. Vespene is designed to combat chaos in complex software development and operations environments. Our mission is simple: get great people together to build the ultimate system we all want to use.
build server

Bragging rights?
adoption

As a company heavily invested in AI, Uber aims to leverage machine learning (ML) in product development and the day-to-day management of our business. In pursuit of this goal, our data scientists spend considerable amounts of time prototyping and validating powerful new types of ML models to solve Uber’s most challenging problems (e.g., NLP based smart reply systems, ticket assistance systems, fraud detection, and financial and marketplace forecasting). Once a model type is empirically validated to be best for the task, engineers work closely with data science teams to productionize and make it available for low latency serving at Uber-scale. This cycle of prototyping, validating, and productionizing is central to ML innovation at Uber, and the less friction at each stage of this process, the faster Uber can innovate.
machine learning

ChessViz reads a chess game as pgn-file and generates a visual representation of the game as chart. The chart shows how the game developed, who was in front, which moves were critical, which moves were forced and different sections (like rookending) of the game are marked. ChessViz is implemented in Python with the packages “python-chess” (https://pypi.org/project/python-chess/) and “plotly” (https://plot.ly/python/).
chess


Projects

kamerka - 416 Stars, 48 Fork
Build interactive map of cameras from Shodan

waveglow - 132 Stars, 21 Fork
A PyTorch implementation of the WaveGlow: A Flow-based Generative Network for Speech Synthesis

mpDNS - 60 Stars, 11 Fork
Multi-Purpose DNS Server

StarGAN-Voice-Conversion - 25 Stars, 0 Fork
This is a tensorflow implementation of the paper: StarGAN-VC: Non-parallel many-to-many voice conversion with star generative adversarial networks https://arxiv.org/abs/1806.02169

instadp - 19 Stars, 5 Fork
Download any users Instagram display picture/profile picture in full quality

XanXSS - 14 Stars, 0 Fork
A simple XSS finding tool

djangohunter - 12 Stars, 6 Fork
Tool designed to help identify incorrectly configured Django applications that are exposing sensitive information.

lswriteups - 12 Stars, 1 Fork
CLI tool to get the links of original writeups from ctftime.org

django-find - 9 Stars, 0 Fork
Easily add search functionality to Django projects

django-auth-tutorial-example - 5 Stars, 2 Fork
Django Authentication Video Tutorial

tfmodel - 4 Stars, 0 Fork
Command-line tool to inspect TensorFlow models

A tiny app adding support unlimited varchar fields (no specified max length) in Django/Postgres.

Wallaroo Labs: Python Python Python! Python 3 Comes to Wallaroo

$
0
0
If you’ve tried to build a scalable distributed system in Python you could be excused for thinking that the world is conspiring against you; in spite of Python’s popularity as a programming language for everything from data processing to robotics, there just aren’t that many options when it comes to using it to create resilient stateful applications that scale easily across multiple workers. About a year ago we created the Wallaroo Python API to help address this problem.

Python Bytes: #103 Getting to 10x (results for developers)

Python Engineering at Microsoft: Python in Visual Studio Code – October 2018 Release

$
0
0

We are pleased to announce that the October 2018 release of the Python Extension for Visual Studio Code is now available. You can download the Python extension from the Marketplace, or install it directly from the extension gallery in Visual Studio Code. You can learn more about Python support in Visual Studio Code in the documentation.

In this release we have closed a total of 49 issues, including:

  • Jupyter support: import notebooks and run code cells in a Python Interactive window
  • Use new virtual environments without having to restart Visual Studio Code
  • Code completions in the debug console window
  • Improved completions in language server, including recognition of namedtuple, and generic types

Keep on reading to learn more!

Jupyter Support with the Python Interactive window

The extension now contains new editor-centric interactive programming capabilities built on top of Jupyter. To get started, make sure to have Jupyter installed in your environment (e.g. set your environment to Anaconda) and type #%% into a Python file to define a Cell. You will notice a “Run Cell” code lens will appear above the #%% line:

Clicking Run Cell will open the Python Interactive window to the right and run your code. You can define further cells or press Shift+Enter to run the current cell and automatically create a new one (or advance to the next cell).

Separately, if you were to open a Jupyter Notebook file (.ipynb), you will now be prompted to import the notebook as Python code:

Cells in the Jupyter Notebook will be converted to cells in a Python file by adding #%% lines. You can run the cells to view the notebook output in Visual Studio code, including plots:

Check out our blog post Data Science with Python in Visual Studio Code for a deep dive of the new capabilities!

Completions in the Debug Console

When stopped at a breakpoint and typing expressions into the debug console, you now get completions appearing:

Completions are based on variables available at runtime in the current scope.

Automatic detection of new virtual environments

The Python extension will now detect new virtual environments that are created in your workspace root while Visual Studio Code is running. You can create virtual environments from the terminal, and they are immediately available to select by clicking on the interpreter selector in the status bar, or by using the Python: Select Interpreter command.

In the screenshot above  a new virtual environment named “env” was created in the terminal, and then set the active environment, as indicated by 'env' in the status bar. Previously you had to reload Visual Studio Code for the new environment to be available.

Other Changes and Enhancements

We have also added small enhancements and fixed issues requested by users that should improve your experience working with Python in Visual Studio Code. The full list of improvements is listed in our changelog; some notable changes include:

Be sure to download the Python extension for Visual Studio Code now to try out the above improvements. If you run into any problems, please file an issue on the Python VS Code GitHub page.

Python Engineering at Microsoft: Data Science with Python in Visual Studio Code

$
0
0

This post was written by Rong Lu, a Principal Program Manager working on Data Science tools for Visual Studio Code

Today we’re very excited to announce the availability of Data Science features in the Python extension for Visual Studio Code! With the addition of these features, you can now work with data interactively in Visual Studio Code, whether it is for exploring data or for incorporating machine learning models into applications, making Visual Studio Code an exciting new option for those who prefer an editor for data science tasks.

These features as currently shipping as experimental. We’re starting our Visual Studio Code investments in the data science space with two main use cases in mind:

  • Exploring data and experimenting with ideas in Visual Studio Code. Just like how you would use Jupyter Notebooks to explore data, with Visual Studio Code you can accomplish the same but using a familiar editor with your favorite settings. You can define and run individual cells using the IPython kernel, visualize data frames, interact with plots, restart kernels, and export to Jupyter Notebooks.
  • Import Jupyter Notebooks into Python code. When it comes time to turn experimentation into reproducible, production-ready Python code, Visual Studio Code can make that transition very easy. Run the “Import Jupyter Notebook” command in the editor and code will be extracted into a Python file, then all the rich features that make you productive are at your fingertips – including AI-powered IntelliSense (IntelliCode), integrated debugger, Visual Studio Live Share, refactoring, multi-file management, and Git source control.

Now, let’s take a closer look at how Visual Studio Code works in these two scenarios.

Exploring data and experimenting with ideas in Visual Studio Code

Above is an example of a Python file that simply loads data from a csv file and generates a plot that outlines the correlation between data columns. With the new Data Science features, now you can visually inspect code results, including data frames and interactive plots.

A few things to note:

  1. Just like how you organize Jupyter Notebooks using cells, you can define code cells in your Python code by using “#%%” and Markdown cells by using “#%% [markdown]”.
  2. “Run cell” links start to light up once “#%%” are detected. The first time the “Run cell” link is clicked (or Shift-enter are pressed), the “Python Interactive” window on the right will show up and a Jupyter server will start in the background. Code in the cells will then be sent to the Jupyter server to execute and results will be rendered in the window.
  3. By using the top toolbar in the Python Interactive window, you can clear results, restart the iPython kernel, and export results to a Jupyter Notebook.
  4. You can navigate back to the source code by clicking on the “Go To Code” button in each cell.

Import Jupyter Notebooks into Python code

If you have existing Jupyter Notebooks that are ready to be turned into production-ready Python modules, simply bring them into Visual Studio Code by running the command “Python: Import Jupyter Notebook”: this will extract Python code as well as Markdown blocks from the notebook, and put everything into a Python file.

Here is an example of a Jupyter Notebook and the generated Python file. Each code cell becomes a code section with annotation “#%%”, and each Markdown cell turns into a comment section with “#%% [markdown]” annotation. Both cell types are runnable in Visual Studio Code, which means you can reproduce the exact same results that you would see in a Jupyter Notebook.

Try it out today

We’re rolling out these features as experimental in the latest Python extension for Visual Studio Code that shipped today. Please give it a try and let us know what you think by taking a 2-minute survey to help shape the features for your needs.

A quick side note, this is an evolvement from the Visual Studio Code Neuron extension that we worked along with students from Imperial College London this summer. The Neuron extension received a lot of positive feedback just recently, and now we’re taking their awesome work further by building more of such capabilities into the Python extension.

Have fun playing with data in Visual Studio Code! 😊

Wingware News: Wing Python IDE 6.1.2: November 8, 2018

$
0
0
This release allows creating a new virtualenv from the New Project dialog, implements VI mode text object operations such as ciw, exits VI insert mode when jk is typed rapidly, improves support for pytest, supports goto-definition links in docstrings, allows setting the main debug file from editor tabs and the Open Files tool, remembers current tab in preferences and properties dialogs, and makes many other improvements.

Fabio Zadrozny: PyDev 7.0 (mypy, black, pipenv, faster debugger)

$
0
0
PyDev 7.0 (actually, PyDev 7.0.3 after some critical bugfixes on 7.0.0) is now available.

Some of the improvements available in this version include:

Mypymay be used as an additional backend for code analysis (see the preferences in the Preferences > PyDev > Editor > Code Analysis > Mypy). It is is similar to the PyLint integration, and will run Mypy whenever a file is saved in the editor.

Blackcan be used as the code-formatting engine (so, it's now possible to choose between the PyDev formatter, autopep8 or Black).

pipenvmay be used for managing virtual environments (so, when creating a new project, clicking to configure an interpreter not listed will present an option to create a new interpreter with pipenv).

Improvements managing interpreters: it's now possible to manage interpreters directly from the editor (so, for instance, doing ctrl+2 pip install django will use pip to install django in the interpreter related to the opened editor -- and it's possible to change pip for condaor pipenvtoo).

Debugger improvements

The debugger is much faster for Python 3.6 onwards (when cython compiled extensions are available).

The performance improvement on the debugger is due to the using the frame eval mode for breakpoints again.

To give some history, that mode was previously disabled on pydevd because it had some issues which made function calls much slower (even though line stepping was zero overhead), but in the end, that overhead could make the performance worse than just using the plain tracing mode. Also, it had a big memory leak and in the cases where the tracing mode had to be reenabled both modes would be active at the same time and performance would suffer quite a bit.

-- all of those should be fixed and it should now perform better or at least on par with the tracing mode with cython in all scenarios... if you like numbers, see: https://github.com/fabioz/PyDev.Debugger/blob/master/tests_python/performance_check.py#L193 for some benchmarks.

p.s.: the improvements in the Debugger were sponsored by Microsoft, as the PyDev Debugger is used as the core of ptvsd, the Python debugger package used by Python in Visual Studio and the Python Extension for Visual Studio Code.

 

Catalin George Festila: Python Qt5 - default icons with QStyle.

$
0
0
This is a simple example of QStyle.
Using the QStyle can solve more issues above this example.
The QStyle can solve more problems than this example.
You can change everything in PyQt5 and Qt.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI, read here.
This is the result of a source code I used to show you how can use default icons from PyQt5.

See this source code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Widget(QWidget):

def __init__(self, parent= None):
super(Widget, self).__init__()
icons = [
'SP_TitleBarMinButton',
'SP_TitleBarMenuButton',
'SP_TitleBarMaxButton',
'SP_TitleBarCloseButton',
'SP_TitleBarNormalButton',
'SP_TitleBarShadeButton',
'SP_TitleBarUnshadeButton',
'SP_TitleBarContextHelpButton',
'SP_MessageBoxInformation',
'SP_MessageBoxWarning',
'SP_MessageBoxCritical',
'SP_MessageBoxQuestion',
'SP_DesktopIcon',
'SP_TrashIcon',
'SP_ComputerIcon',
'SP_DriveFDIcon',
'SP_DriveHDIcon',
'SP_DriveCDIcon',
'SP_DriveDVDIcon',
'SP_DriveNetIcon',
'SP_DirHomeIcon',
'SP_DirOpenIcon',
'SP_DirClosedIcon',
'SP_DirIcon',
'SP_DirLinkIcon',
'SP_FileIcon',
'SP_FileLinkIcon',
'SP_FileDialogStart',
'SP_FileDialogEnd',
'SP_FileDialogToParent',
'SP_FileDialogNewFolder',
'SP_FileDialogDetailedView',
'SP_FileDialogInfoView',
'SP_FileDialogContentsView',
'SP_FileDialogListView',
'SP_FileDialogBack',
'SP_DockWidgetCloseButton',
'SP_ToolBarHorizontalExtensionButton',
'SP_ToolBarVerticalExtensionButton',
'SP_DialogOkButton',
'SP_DialogCancelButton',
'SP_DialogHelpButton',
'SP_DialogOpenButton',
'SP_DialogSaveButton',
'SP_DialogCloseButton',
'SP_DialogApplyButton',
'SP_DialogResetButton',
'SP_DialogDiscardButton',
'SP_DialogYesButton',
'SP_DialogNoButton',
'SP_ArrowUp',
'SP_ArrowDown',
'SP_ArrowLeft',
'SP_ArrowRight',
'SP_ArrowBack',
'SP_ArrowForward',
'SP_CommandLink',
'SP_VistaShield',
'SP_BrowserReload',
'SP_BrowserStop',
'SP_MediaPlay',
'SP_MediaStop',
'SP_MediaPause',
'SP_MediaSkipForward',
'SP_MediaSkipBackward',
'SP_MediaSeekForward',
'SP_MediaSeekBackward',
'SP_MediaVolume',
'SP_MediaVolumeMuted',
'SP_CustomBase'
]
Col_size = 6

layout = QGridLayout()

count = 0
for i in icons:
select_button = QPushButton(i)
select_button.setIcon(self.style().standardIcon(getattr(QStyle, i)))

layout.addWidget(select_button, count / Col_size, count % Col_size)
count += 1

self.setLayout(layout)

if __name__ == '__main__':
app = QApplication(sys.argv)

dialog = Widget()
dialog.show()

app.exec_()

Talk Python to Me: #185 Creating a Python 3 Culture at Facebook

$
0
0
Do you or your team maintain a large Python 2 code base? Would you like to move to Python 3 but there's just too much in place keeping you on legacy Python? Then you will definitely enjoy this story from Jason Fried. He created a grassroots campaign to move Facebook's massive Python 2 codebase to Python 3 and he made Python 3 part of the culture. There are lessons here for every listener.

PyBites: Code Challenge 57 - Analyze Olympic Games Data With Pandas - Review

$
0
0

In this article we review last week's Analyze Olympic Games Data With Pandas code challenge.

Community Pull Requests

Another 12 PRs this week, cool!

[bobbelderbos@imac challenges (community)]$ git pull origin community
...
From github.com:pybites/challenges
 * branch            community  -> FETCH_HEAD
   028277e..3873c66  community  -> origin/community
Updating 028277e..3873c66
Fast-forward
...
 45 files changed, 37652 insertions(+), 2 deletions(-)

Check out the awesome PRs by our community for PCC57 (or from fork: git checkout community && git merge upstream/community):

Read Code for Fun and Profit

You can look at all submitted code here and/or on our Community branch.

Here are the Pythonic learnings we spotted in Pull Requests made during the last week:

PCC03

Learned about difflib SequenceMatcher and collections Counter. - PR

PCC04

I finally worked with an API in python, I found little more information about working of unittests, got to know little more about CSV module. - PR

PCC16

learned about the IP address lookup API - PR

I learn how to make http request to remote database (in this project used RIPE DB) and how to parse JSON output from DB - PR

Learned how to use PRAW module to interact with Reddit API. Managed to get the top topic submissions per valid subreddit topic. - PR

PCC26

I learned how to use PyQt5, python GUI framework. I managed to get familiar on views events, especially on click. Also, managed to play with PyQt5.QtWebEngineWidgets to load a webpage of a given URL - PR

PCC54

Gained more experience representing datetime objects as strings in the format I want them, used error handling to make my code more concise, and found the useful string method splitlines() which gives a list of all strings separated by newlines. I can easily insert an element into the list and then reform the list into file contents. This is much easier than parsing through a string of all the file contents to find a certain location. - PR

PCC57

Nice one to get back into Pandas, this helped: https://pybit.es/guest-marvel-data-analysis.html - still some work to be done like merging countries and more plotting, but moving on for now. - PR

Definitely. Learned that I still have a lot to learn about Pandas :) - PR

The Git was the most challenging part - but good practice! I got a little distracted trying to solve new problems. - PR


Thanks to everyone for your participation in our blog code challenges! Keep the PRs coming and include a README.md with one or more screenshots if you want to be featured in this weekly review post.

Become a Python Ninja

Master Python through Code Challenges:

  • Subscribe to our blog (sidebar) to get a new PyBites Code Challenge (PCC) in your inbox every week.

  • Take any of our 50+ challenges on our platform.

  • Prefer coding bite-sized Python exercises in the comfort of your browser? Try our growing collection of Bites of Py.

  • Want to do the #100DaysOfCode but not sure what to work on? Take our course and/or start logging your 100 Days progress using our Progress Grid Featureon our platform.


Keep Calm and Code in Python!

-- Bob and Julian

PyBites: Code Challenge 58 - Analyze Podcast Transcripts with NLTK - Part I

$
0
0

There is an immense amount to be learned simply by tinkering with things. - Henry Ford

Hey Pythonistas,

It's time for another code challenge! In this two part challenge we're going to do some natural language processing on podcast transcript data. Prepare to have fun expanding your data science skills!

The Challenge

Here are the steps you would follow:

  • Week 1

    • Pick your favorite podcast, make sure it is one with quite some data (episodes) and transcripts.
    • Make a script to get all the transcripts. As this could involve retrieving data from Github (Talk Python), feed parsing / web crawling, even extracting data from PDFs (Tim Ferriss Show - episodes 1-150), we decided to split this into two challenges.
    • Store the results somewhere, for example an (sqlite) database.
    • This week you can PR this prep work via this link
  • Week 2

    • Make a virtual environment and pip install NLTK / Natural Language Toolkit.
    • Read up on how to use the library.
    • From here on we leave you totally free to find the patterns in the data that you are interested in: sentiments, book recommendations, you name it.
    • Show the results in a notebook or in any way you like.
    • PR link to be released next Monday ...

Good luck and have fun!

Ideas and feedback

If you have ideas for a future challenge or find any issues, open a GH Issue or reach out via Twitter, Slack or Email.

Last but not least: there is no best solution, only learning more and better Python. Good luck!

Become a Python Ninja

At PyBites you get to master Python through Code Challenges:

  • Subscribe to our blog (sidebar) to get a new PyBites Code Challenge (PCC) in your inbox every week.

  • Apart from this blog code challenge we have a growing collection of 50+, check them out on our platform.

  • Prefer coding bite-sized Python exercises in the comfort of your browser? Try our growing collection of Bites of Py.

  • Want to do the #100DaysOfCode but not sure what to work on? Take our course and/or start logging your 100 Days progress using our Progress Grid Featureon our platform.


>>>frompybitesimportBob,JulianKeepCalmandCodeinPython!

PyCharm: PyCharm 2018.3 Release Candidate

$
0
0

PyCharm 2018.3 is almost ready to be released, and that’s why we’re happy to present the release candidate for PyCharm 2018.3. Download this version from our website

New in This Version

Multiline TODOs

Multiline Todos

If you like to put TODOs in your code to remember yourself (or your colleagues) to further improve a part of your code, you now have more space to do so. If you indent the line after the line with ‘TODO’ (or ‘FIXME’, or anything you configure in Settings | Editor | Todo) it’ll be treated as a continuation of the todo.

Further Improvements

  • Various Docker Compose fixes
  • An issue where PyCharm would freeze when using Ctrl-C in the Python console has been resolved
  • Debugging node.js code on WSL could in some cases result in an exception, this is now fixed
  • And more, read the release notes

Interested?

Download the RC from our website. Alternatively, you can use the JetBrains Toolbox App.

If you’re on Ubuntu 16.04 or later, you can use snap to get the PyCharm RC, and stay up to date. You can find the installation instructions on our website.

The release candidate is not an EAP version, this means that you will either need to have an active PyCharm license, or you’ll receive a 30 day free trial for PyCharm Professional Edition. The community edition is free and open source as usual.

Continuum Analytics Blog: Choose Your Anaconda IDE Adventure: Jupyter, JupyterLab, or Apache Zeppelin


Stack Abuse: How to Create, Move, and Delete Files in Python

$
0
0

Introduction

Handling files is an entry-level and fundamental skill for any programmer. They're very commonly used to store application data, user configurations, videos, images, etc. There are a countless number of use-cases for files in software applications, so you'd be smart to make yourself deeply familiar with the tasks of manipulating files. These tasks could include (among others) creating, deleting, and moving files.

In this article, we'll cover the process of working with files using the Python programming language. The built-in methods it offers makes it very easy to handle files using a relatively small amount of code. As with anything in programming, there are many ways to achieve the same goal when it comes to files, but in this article we'll stick with the basics and show the most common ways to perform these actions.

Creating Files in Python

File Opening Modes

There are modes in which you can open a file in Python. The mode you choose depends on how you plan to use the file, or what kind of data you'll be reading (writing) from (to) the file. This mode is specified when opening a file using the built-in open() method, explained in further detail in the next section.

Let's take a look at some of the possible combinations of file modes:

  • w: Opens a file for writing and creates a new file if it doesn't yet exist. In the case that the file does exist, it overwrites it.
  • w+: Opens a file for writing but also for reading and creating it if it doesn't exist. If a file already exists, it overwrites it.
  • r: Opens a file for reading only.
  • rb: Opens a file for reading in Binary format.
  • wb: Opens a file for writing in Binary format.
  • wb+: Opens a file for writing and reading in Binary format.
  • a: Opens a file for appending at the end of the file.
  • +: In general, this character is used along side r, w, or a and means both writing and reading.

If no file mode is specified, then r will be assumed by default.

When choosing a mode, take in to careful consideration what your use-case is and what all the file will be used for for the duration that it is open.

open()

As with just about anything in Python, performing many file-related tasks is very simple. For example, creating a file in Python can be done in a single line of code:

myFile = open("new.txt", "w+")  

In the example above we have opened a file, "new.txt" for reading and writing. In our case, since the file does not already exist, it is automatically created.

The open() method accepts many arguments, although the majority of the time you'll only use these two:

  • filename– Required field to specify the name of the file we wish to open/create. In our example, "new.txt"
  • mode– Optional argument to specify the file opening mode, in our example "w+"

You can omit the second argument, in which case it will be assumed by Python as r.

The other arguments not listed here allow you to configure buffering, encodings, newline handling, etc. For more info on these arguments, check out the official Python documentation on the open method.

close()

When you open a file you always need to make sure that you also close it. This can be done using the file object's close() method, or opening it using the with keyword that Python provides, which closes it automatically when out of scope. After closing the file, it won't be available for reading or writing within your code anymore, unless you open it back up.

Keeping your files open will use up system resources, slow down your program, and in some cases prevent the file from being used by other code. Python's garbage collection does it's best to automatically close files when they're not being used anymore, but you shouldn't rely on it.

If a file is already closed, calling close() on it again won't affect it at all:

myFile.close()  
myFile.close() # Doesn't affect the file  

Moving Files in Python

To move a file in Python, we will need to import the os and shutil modules that provide us the ability to copy, move, and remove files in Python. Both of these modules provide methods to do so, although in many cases the shutil module has more convenient methods.

import os  
import shutil

# Move a file by renaming it's path
os.rename('/Users/billy/d1/xfile.txt', '/Users/billy/d2/xfile.txt')

# Move a file from the directory d1 to d2
shutil.move('/Users/billy/d1/xfile.txt', '/Users/billy/d2/xfile.txt')  

Keep in mind that the destination directory needs to exist in order for this example to work. Once you've set up the directories "d1" and "d2" (or just changed the example to fit your directory structure), run the code. Now check out the "d2" directory and you should see the xfile.txt if now present in that directory.

Pretty simple, right?

Deleting Files in Python

As you probably guessed, it's pretty easy to remove a file in Python using the remove() method from the os module.

In our example below, we'll delete the "xfile.txt". All we need to do is call the remove() method with the path of the file we want to delete:

import os

# Delete xfile.txt
os.remove('/Users/billy/d2/xfile.txt')  

Now check out the "d2" directory again and the file xfile.txt will now be done. Simple as that!

Conclusion

In this article, we showed very simple examples of how to create, move, and delete files in Python using the built-in functions such as open(), shutil.move(), and os.remove(). In addition, we presented a simple introduction and explanation of Python file modes.

A. Jesse Jiryu Davis: API Evolution the Right Way

$
0
0
Imagine you are a creator deity, designing a body for a creature. In your benevelonce, you wish for the creature to evolve over time: first, because it must respond to changes in its environment, and second, because your wisdom grows and you think of better designs for the beast. It shouldn’t remain in the same body forever! The creature, however, might be relying on features of its present anatomy. You can’t add wings or change its scales without warning.

Brad Lucas: Updating Your Pip Requirements File

$
0
0

While revisiting an old Python project you may come to realize that your requirements.txt file contains out of date versions. You could go through and research each library and get it's latest version number but may be discouraged if there too many.

The following is a technique to update the entire file without having to take too much time.

This technique is good if your goal to to update each library to it's most recent first.

=_for_each_entry">=_for_each_entry">=_for_each_entry">Step 1 Change the == to >= for each entry

This will unpin the reference to a specific version

Old requirements.txt

amqp==1.4.9
anyjson==0.3.3
BeautifulSoup==3.2.1
billiard==3.3.0.23
celery==3.1.13
certifi==2018.1.18
chardet==3.0.4
Django==1.6.6
django-extensions==1.3.11
django-celery==3.1.1
idna==2.6
kombu==3.0.37
pytz==2018.3
requests==2.18.4
six==1.7.3
urllib3==1.22
vine==1.1.4

Edited requirements.txt

amqp>=1.4.9
anyjson>=0.3.3
BeautifulSoup>=3.2.1
billiard>=3.3.0.23
celery>=3.1.13
certifi>=2018.1.18
chardet>=3.0.4
Django>=1.6.6
django-extensions>=1.3.11
django-celery>=3.1.1
idna>=2.6
kombu>=3.0.37
pytz>=2018.3
requests>=2.18.4
six>=1.7.3
urllib3>=1.22
vine>=1.1.4

Step 2 Run pip with the upgrade flag

$ pip install -r requirements.txt --upgrade

Step 3 Verify the new requirements

$ pip list

Step 4 Create a new requirements.txt file

$ pip freeze > requirements.txt

Weekly Python StackOverflow Report: (cli) stackoverflow python report

$
0
0

Catalin George Festila: Python Qt5 - QTabWidget example.

$
0
0
Today I test the QTabWidget with an simple example.
The source code uses the QTabWidget and create two Tab: FirstTab and TabTwo.
I add for each tab two labels: tab_one_label_one, tab_one_label_two,tab_two_label_one and tab_two_label_two.
For layouts, I used: vboxLayout with first_layout and second_layout.
The result of my running code is this:

The rest of the source code is simple:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDialog, QApplication, QWidget
from PyQt5.QtWidgets import QVBoxLayout, QTabWidget, QLabel

class TabDialog(QDialog):
def __init__(self):
super().__init__()

self.setWindowTitle("Tab Widget Application")
self.setWindowIcon(QIcon("icon.png"))

tabwidget = QTabWidget()
tabwidget.addTab(FirstTab(), "First Tab")
tabwidget.addTab(TabTwo(), "Second Tab")

vboxLayout = QVBoxLayout()
vboxLayout.addWidget(tabwidget)

self.setLayout(vboxLayout)

class FirstTab(QWidget):
def __init__(self):
super().__init__()
tab_one_label_one = QLabel("tab_one_label_one")
tab_one_label_two = QLabel("tab_one_label_two")

first_layout = QVBoxLayout()
first_layout.addWidget(tab_one_label_one)
first_layout.addWidget(tab_one_label_two)
self.setLayout(first_layout)

class TabTwo(QWidget):
def __init__(self):
super().__init__()
tab_two_label_one = QLabel("tab_two_label_one")
tab_two_label_two = QLabel("tab_two_label_two")

second_layout = QVBoxLayout()
second_layout.addWidget(tab_two_label_one)
second_layout.addWidget(tab_two_label_two)
self.setLayout(second_layout)

if __name__ == "__main__":
app = QApplication(sys.argv)
tabdialog = TabDialog()
tabdialog.show()
app.exec()
Viewing all 22647 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>