In pycharm how to change unit test to pytest in this dialog - python

When I open a new Python file, this small dialog gives the option to make a unit test template as is in the picture. I want to change this shortcut to instead create a pytest one.
How can i do that?
And, can I put more "templates/shortcuts" in this dialog? That will save time on some templates that I use a lot to start new projects.
I already installed pytest, and went through this:
Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework.
In the Default test runner field select pytest.
But even with pytest as default, the dialog still shows "unit test" only and not pytest.

As far as I know, the selection menu for the Python file type that you get if you select New...|Python file is not configurable. You can, however, add a file template yourself, that will appear in the list of file types you get if you select New....
The easiest way to access this is via New...|Edit File Templates..., or you can get to the same dialog via Settings...|Editor|File and Code Templates. You can add templates for all new projects (schema Default), or for the current project only (schema Project), though that makes sense only for large projects.
Note that your custom file templates are always ordered alphabetically in the list, so if you want to show them in a specific order, you may use some prefix in the template names to enforce the wanted order.
Note:
I checked in PyCharm code where the internal file templates (e.g. the templates in the "New Python file" dialog) are handled, and this seems indeed to be hardcoded. Short of adapting the PyCharm code there seems to be no possibility to configure this.

Related

Register plugin for only one directory in python project

I have this python project with three directories, my_dir1, my_dir2, and my_dir3. Within my_dir1 I have defined a plugin that I would like to use for my_dir2 but not my_dir3. The problem is that even though I have configured the conftest.py for my_dir2 to register the plugin, it seems to be applied to my_dir3 as well. Since I do not want the plugin to apply to my_dir3, how do I prevent this from happening?
P.S. I should add that methods in my_dir3 are invoking methods in my_dir2

Using a Python Script in My Hype 3 Build

I have a Python script that I would like to run in my Hype 3 build. The script takes an input (several) and outputs an answer based on on the input (but that’s handled in the script) how I could do that?
Tumult Hype’s Export Scripts infrastructure allows code to be run upon export and/or preview. This can be python code, in fact the sample scripts we have are all in Python.
General info: https://tumult.com/hype/export-scripts/
Developer docs & code: https://github.com/tumult/hype-export-scripts/
With this, you can arbitrarily modify Hype’s output however you see fit.
You’ll get a new File > Export as HTML5 > … menu item; this allows choosing a location to save. I don’t think there’s any way to bypass the save dialog at this point. You probably just ignore it. (Though I guess you could also be clever and have a Preview stick your document wherever you want, since previewing doesn’t have a prompt).

Using mixin with the online Pony editor output

The Pony documentation suggests to use mixins in order to add methods to the classes automatically generated by the online editor.
The documentation shows the manually created mixins.py containing the custom methods and the automatically generated models.py containing the line class Product(db.Entity, ProductMixin):.
How do I tell the editor to automatically add , ProductMixin?
Adding it manually would defeat the purpose, because it would require to manually modify models.py in order to avoid manually modifying models.py.
Am I missing something?
You are right, this feature is not supported by the editor yet.
At this point we are preparing the PonyORM release which will include the migration tool. Once we release it, the next thing we'll do is add the feature you're talking about to the editor.
You'll be able to download a .zip file from the editor, which includes the Pony project files, such as models.py, mixins placeholder, database configuration file.
Thank you for your question.

PyCharm "Run configuration" asking for "script parameters"

While writing an application parsing command line arguments I would like to run it with various parameters.
I don't want to create a Run Configuration for every possible command line argument that I want my script to test with. Is there a way in PyCharm (and I guess with any JetBrains IDE) to make a Run Configuration that asks for the Script parameters when executed?
I am currently using PyCharm 3.1 (EAP).
Currently the only possibility is to use the "Before launch | Show this page" option.
I've found today that now is possible to ask for parameters using the "Prompt" macro on the "Run configuration" parameters field.
https://www.jetbrains.com/help/pycharm/code-running-assistance-tutorial.html#parameter-with-macros
Although yole's answer is the de facto way to be prompted for thw arguments before running a program, it is slightly annoying because:
the dialog is visually overwhelming and cluttered instead of focused on what you want to do;
you have to tab to reach the arguments field if you want to use the keyboard exclusively (and why not?);
Nothing you could do about that. (Except maybe file a ticket. Have you done that?)
I'm just adding what I used to do before I knew about Googled for this option for the sake of completeness (obvously, this is a hack in the least glamorous sense of the term). But it did suit my workflow as I often only had discrete lines to test with, and didn't switch that often.
Create a new configuration set to the same file, but with a special 'magic' parameter;
Add code to your script to check if the magic is there;
Use a string variable instead of sys.argv (pass it through lambda args: [__name__] + args.split() to reduce the boilerplate);
???
Profit;
I'm doing this on a Mac, but hopefully this will be helpful for Windows or Linux.
Go to Run > Edit Configurations
There will be a dialog box that opens.
Script: file you want to run (ending with .py)
Script Parameters: the command line arguments
Working Directory: directory where your project is.
My simple answer is adding another wrapper as the cover in the source code which will run on the selection you made through code branch or external command or file, so choosing different branch is just a 'ddp' tap distance in vim(line change for parameter settings). You dont have to depend on pycharm updating by building your own code world:)

How to implement a settings file in a Python program

A "settings file" would be a file where things like "background color", "speed of execution", "number of x's" are defined. Currently, I implemented it as a single setting.py file, which I import in the beginning. Someone told me I should make it a settings.ini file instead, but I don't see why! Care to clarify, what is the optimal option?
There is no optimal solution; it is a matter of preference.*
Normally, settings do not need to be expressed in a Turing-complete language: they're often just a bunch of flags and options, sometimes strings and numbers, etc. An argument for having a settings.py file (though very unorthodox) would be if the end-user was expected to write code to generate very esoteric configurations (e.g. maps for a game). This would then be fairly similar to shell script .bashrc-style files.
But again, in 99.9% of programs, the settings are often just a bunch of flags and options, sometimes strings and numbers, etc. It's fine to store them as JSON or XML. It also makes it easy to perform reflection on your settings: for example, automatically listing them in a tree manner, or automatically creating a GUI out of the descriptions.
(Also it may be a (unlikely?) security issue if you allow people to inject code by modifying the settings file.)
*edit: no pun intended...
There are a few reasons why separating out config files from main codebase is a good idea. Of course it depends on your use case and you should evaluate against your usecase.
Configuration can be managed by end user, who do not understand programming languages. It makes more sense to factor out configuration and use a simple ini file which uses simple key-value pairs for config parameters.
Configuration varies based on the installation environment. Your code runs on multiple environment and they all use different configuration. It is very easy to maintain such cases by having separate config files and same source code installed on those environments.
There are package managers that knows what is a config file and what is a source file. They are intelligent to not override any changed config on version upgrade etc. So you do not have to worry about resetting config parameters after version upgrade of package. For example you ship your product with a default config file. User fine tuned few parameters. You shipped another version of the package. User should not expect a config reset after version upgrade.
One problem with having a settings file being a Python module is that it can contain code that will be executed when you import it. This may allow malicious code to be inserted into your program.
For Python use stock libraries:
YAML style configuration files:
http://www.yaml.org/start.html
http://pypi.python.org/pypi/PyYAML/
(Used e.g. Google App Engine)
INI: http://docs.python.org/library/configparser.html
Don't use XML for hand-edited config files.

Categories