python: naming a module that has a two-word name - python

I'm trying to put together a really simple module with one .py source file in it, and have already run into a roadblock. I was going to call it scons-config but import scons-config doesn't work in Python. I found this SO question and looked at PEP8 style guide but am kind of bewildered, it doesn't talk about two-word-name conventions.
What's the right way to deal with this?
module name: SconsConfig? scons_config? sconsconfig? scons.config?
name of the single .py file in it: scons-config.py? scons_config.py?
edit: I did see "the use of underscores is discouraged" and that left me at a dead end: should I use "sconsconfig" or "scons_config" (I guess the other ones are out)?

If you have to, always use underscores _.
Using a dot . would not even work, otherwise
from scons.config import whatever
would break.
But PEP 8 clearly describes it:
Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used
in the module name if it improves readability. Python packages should
also have short, all-lowercase names, although the use of underscores is
discouraged.
UPDATE:
To directly target your question: I think sconsconfig is fine. It is not too long and quite readable.
But honestly, I don't think anyone will blame you if you use underscores and your code will run with either decision. There is always a certain level where you should not care that much anymore.

First, the module name is the same as the name of the single .py file. In Python-speak, a collection of several .py files is a package.
PEP-8 discourages breaking up package names with underscores. A quick peak at my site-packages directory shows that multiword names are commonly just run together (e.g., setuptools, sqlalchemy)
Module names (that is, file names) may be broken up by underscores (and I usually do this, because I hate namesthatruntogethersoyoucanhardlyreadthem).
Stick with lower-case only (per PEP-8). This avoids problems when going from case-sensitive to case-insensitive filesystems and vice versa.

Aside from PEP-8, you can also check out how the native Python modules deal with this issue.
If you were to compare the native modules of Python 2 to that of Python 3, you would see that the new tendency with the official devs is to avoid uppercase and underscores. For example, ConfigParser in Python 2 becomes configparser in Python 3.
Looking at this, the best course of action would be to avoid uppercase and underscores, and just join the words together, i.e. sconsconfig.

- is a no go. The symbol is used for minus operator. The same is true in most programming languages. Use _ or otherwise nothing at all.

Related

Python: how to batch rename mixed case to lower case with underscores

I've written a fair bit of my first significant Python script. I just finished reading PEP 8, and I learned that lower_case_with_underscores is preferred for instance variable names. I've been using mixedCase for variable names throughout, and I'd like my code to be make more Pythonic by changing those to lower_case_with_underscores if that's how we do things around here.
I could probably write some script that searches for mixedCase and tries to smartly replace it, but before I potentially reinvent the wheel, my question is whether a solution for that already exists, either within a Python-savvy editor or as a standalone application; or whether there's another approach that would accomplish the task of converting all mixedCase variable names to lower_case_with_underscores. I have searched a fair bit for a solution but didn't turn up anything. Any technique that specifically would yield this result would be appreciated.
I was able to accomplish what I wanted using GNU sed:
sed -i -e :loop -re 's/(^|[^A-Za-z_])([a-z0-9_]+)([A-Z])([A-Za-z0-9_]*)'\
'([^A-Za-z0-9_]|$)/\1\2_\l\3\4\5/' -e 't loop' myFile.py
It finds every instance of mixedCase -- but not CapitalWords, so class names are left intact -- and replaces it with lower_case_with_underscores; e.g. myVariable becomes my_variable, but MyClass remains MyClass.
(On an unrelated note, now that I've done it, I think I prefer the appearance of mixedCase over lower_case_with_underscores. The appearance of so many underscores all over my code is weird. But I'll do things the Python Way and see if I get used to it, particularly if I intend for my code to be seen or worked with by others; or maybe I'll do it the way I like and I've now got a simple way of converting it to the PEP 8 way if I intend to make the code public.)

Programming in Python using a Non-English language for keywords and variables

My end goal is to enable people that know the language Urdu and not English to be able to program in a Python environment.
Urdu is written left to right. I imagine having Urdu versions of all python keywords and using Urdu characters to define variable/function/class names.
Is this goal possible? If yes, then what all will need to be done?
I can already see one issue where the standard library functions would still be in English.
Update:
Whether this should be done or not is certainly a very interesting debate topic, which I'd love to talk about. However, is it actually possible and how?
I love Python and I know a lot of intelligent people that might never be taught English properly, only Urdu, hence the thought.
Chinese Python used to allow programmers to write source code entirely in Chinese, including translated module names, function names, and all keywords. Here's a code example from their website:
載入 系統 # import sys
文件名 = 系統.參數[1:] # filenames = sys.argv[1:]
定義 修正行尾(文件): # def fixline(file):
內文 = 打開(文件).讀入() # text = open(file).read()
內文 = 內文.替換('\n\r','\n') # text = text.replace('\n\r', '\n')
傳回 內文 # return text
取 文件 自 文件名: # for file in filenames:
寫 修正行尾(文件) # print fixline(file)
Sadly, it is no longer an active project, but you can get the source and find out what they changed to get an idea for your own implementation. (Learning why they failed may also help you understand what challenges you will have in making such a system successful).
You can use the ideas project to transform keywords,
it has an example for making a kind of French Python.
It's based on python import-hook so it runs in normal python3.
I built a python in Hebrew (that also uses the python builtins and not just keywords ) using this project ,that looks more like a normal python
Yes, it is possible. But it requires you to download Python from source, and change the source code and compile it. If I remember correctly from another discussion on the same topic, changing the grammar and regenerating some files is all it takes, except for when you are doing advanced stuff like meta-programming and things.
WHAT HAVE YOU TRIED? Python 3 allows you to use Unicode for all your function, class and variable names. Check it out, you just need to make sure you're using utf-8 for your script-- it's primarily a matter for your editor. Dealing gracefully with the mixed RTL/LTR issues is also your editor's problem. (The feature is discussed in PEP 3131)
The python language does not have "dialects", i.e., alternative sets of keywords. If you are not satisfied with Urdu identifiers and you are determined to have a completely Urdu-language experience, you could write a preprocessor that maps Urdu "keywords" to the corresponding (English) python keyword.
It shouldn't be too hard to wrap this kind of preprocessor around the interactive console and import modules, so that it works transparently to the user (and without recompiling python from source). If you can program and want to try this on for size, check out python's code module. It's designed to read python source and send it on to be compiled and executed. You just step in and add a preprocessing step.
I'd use a preprocessor for keywords and built-ins. But providing localized wrappers for your choice of common modules is even easier, actually. Let's demonstrate with a wrapper module RE.py that contains all exported identifiers of regular re, but renamed to upper case:
import re as _re
for name in _re.__all__:
locals()[name.upper()] = _re.__dict__[name]
That was it! You can now import this module and use it:
import RE
docs = RE.SUB(r"\bEnglish\b", r"Urdu", docs)
Use Urdu words instead of uppercase (of course you need to specify each one individually) and you're on your way. As you say, whether all this is a good idea is a different question.

Python module with a dash, or hyphen (-) in its name

I have an existing python module with a dash in its name, foo-bar.py
Changing the module name is something I would prefer to avoid as the module is shared, and I would have to chase down all the places it is used so that my special case will work.
Is there a way to load a module whose name contains the typically forbidden '-'?
(I do understand that this isn't a best practice. But for this situation I would prefer not to redesign and test a much larger set of applications. Also I don't think my corporate masters would approve of my taking the time to implement such a change.)
You can do that using __import__(). For example:
foobar = __import__("foo-bar")
But you really should rename the module instead. That way you can avoid confusion where the filename of the module is different from the identifier used in the program.
I know this question has already been answered to satisfaction of the asker, but here is another answer which I believes has some merit above using __import__().
import importlib
mod = importlib.import_module("path.to.my-module")
# mod.yourmethod()
According to the docs:
"This provides an implementation of import which is portable to any
Python interpreter. This also provides an implementation which is
easier to comprehend than one implemented in a programming language
other than Python."
Python 2.7 + only

When should a Python script be split into multiple files/modules?

In Java, this question is easy (if a little tedious) - every class requires its own file. So the number of .java files in a project is the number of classes (not counting anonymous/nested classes).
In Python, though, I can define multiple classes in the same file, and I'm not quite sure how to find the point at which I split things up. It seems wrong to make a file for every class, but it also feels wrong just to leave everything in the same file by default. How do I know where to break a program up?
Remember that in Python, a file is a module that you will most likely import in order to use the classes contained therein. Also remember one of the basic principles of software development "the unit of packaging is the unit of reuse", which basically means:
If classes are most likely used together, or if using one class leads to using another, they belong in a common package.
As I see it, this is really a question about reuse and abstraction. If you have a problem that you can solve in a very general way, so that the resulting code would be useful in many other programs, put it in its own module.
For example: a while ago I wrote a (bad) mpd client. I wanted to make configuration file and option parsing easy, so I created a class that combined ConfigParser and optparse functionality in a way I thought was sensible. It needed a couple of support classes, so I put them all together in a module. I never use the client, but I've reused the configuration module in other projects.
EDIT: Also, a more cynical answer just occurred to me: if you can only solve a problem in a really ugly way, hide the ugliness in a module. :)
In Java ... every class requires its own file.
On the flipside, sometimes a Java file, also, will include enums or subclasses or interfaces, within the main class because they are "closely related."
not counting anonymous/nested classes
Anonymous classes shouldn't be counted, but I think tasteful use of nested classes is a choice much like the one you're asking about Python.
(Occasionally a Java file will have two classes, not nested, which is allowed, but yuck don't do it.)
Python actually gives you the choice to package your code in the way you see fit.
The analogy between Python and Java is that a file i.e., the .py file in Python is
equivalent to a package in Java as in it can contain many related classes and functions.
For good examples, have a look in the Python built-in modules.
Just download the source and check them out, the rule of thumb I follow is
when you have very tightly coupled classes or functions you keep them in a single file
else you break them up.

Can you separate python projects logically into separate files/classes like in C#/Java?

I'm looking to develop a project in python and all of the python I have done is minor scripting with no regard to classes or structure. I haven't seen much about this, so is this how larger python projects are done?
Also, do things like "namespaces" and "projects" exist in this realm? As well as object oriented principles such as inheriting from other classes?
Yes, you can, and you should! :)
Here is a nice introduction to Python Modules (including packages).
Correction: you probably should not put each single class into a separate file (like Java mandates and many C++ places do). The language is pretty lax about it as you can see in the linked tutorial, keep an open eye on other projects, use common sense, and do whatever makes sense to you (or whatever is being done in your team/project - unless it is very wrong).
You can put code (classes, function defs, etc) into python modules (individual source files), which are then imported with import. Typically like functionality (like in the Python standard library) is contained within a single module.
You can do that, but usually they are arranged a bit different.
You can take a look to the source code of one python application.
Here's one: "JaikuEngine" which powers the website http://www.jaiku.com/
Yes.
You can put python classes into separate files, use namespaces for scoping, then place this into Modules which can be loaded from other scripts.

Categories