Pythonic: code name conflicting with built-in - package

I'm currently creating a code named "SET". The code's name is an acronym, which has been defined for many (non programming) reasons, and therefore cannot be changed.
Problem: The easiest, and I believe the less painful way for the end-user to use my code would be naming the package "set".
But of course this is a problem since this conflicts with the built-in set function.
Question: What are the possible solutions? Some may be (there is probably more):
change the package name (eg. setb).
import setb
I would really really prefer not to, because then it will be different from the real name
make the package's name upper-case (SET)
import SET
It would be a straight forward solution, but I'm wondering: is this a pythonic proper naming for a package? Also, I find this a bit painful since all modules defined in the code will have something like "import SET.x.y..." (ie. upper-case, written a lot of times). But this is not a really big deal if this is a pythonic way.
keep the name "set"
import set
Well this is obviously not fine. But it would be a problem only if the user is using "import set", would not it be? This should not happen in "normal usage conditions", since the code will provides some scripts to use it, rather use it as a standard python module. But we never know, and it could be imported as it, and there may be even some problems I'm not seeing (with the built-in set).
I'm considering the solution 2., but I'm really not sure. Maybe this is not proper, or maybe you guys have a better solution.
PS: I've found some similar topics on the web and on stackoverflow, but it usually deals with names inside a script or module inside a package. The problem here is really related to the code's name (which is meaningful only written this way), and therefore related to the proper naming of the package's name.
EDIT
Selected solution: I've choosen to use "SET" as the package name. Although many good suggestions have been proposed here: pyset, semt, setool... or the more explicit "starexoplanettool" (expliciting the acronym). Thanks to you all.
EDIT #2
I like the "funny" solution of having a package named S, and a subpackage, E... to get finally:
import S.E.T
Thanks Don Question.

Why not spell out the meaning behind the acronym, and if the user is desperate for a shorter name, they can do import someetymologyterm as SET or whatever they prefer. Python gives them the choice, so it's not the end of the world either way.

Is this a pythonic proper naming for a package?
Under the circumstances, it doesn't matter.
Leading upper case is rare (ConfigParser, HTMLParser). Camel-case is rare (cStringIO).
But that doesn't make all upper-case wrong.
It just means you should pick a better acronym next time.
"set" is the English word with the largest number of definitions. It is the single poorest choice of acronym possible.
The point is not to conform to a community "standard" of more-or-less acceptable behavior.
The point is to write something that works.
(ie. upper-case, written a lot of times).
Hardly a concern. People actually run software more often than they write it.
People read and tweak more often than writing, also.
If you're worried about misspelling, there's copy and paste.
Also, even a cheap IDE like Komodo Edit can figure out how to code-complete an installed module with a long name.

If you have to choose such a misfortunate name, you could make the pain in the ass an ironic anathema! Just create a main-package "S" with a subpackage "E" and therein a subpackage "T".
Then you could do an:
import S.E.T
or
from s.e.t import xyz
or
import s.e.t as set_
you could even do some vodoo in the __init__.py file of the main-pacakge ;-)
Just my 2 cents!

Maybe I'm not getting it but this looks simply like a namespace problem.
Here is one way out. Prepend the path to the set.py onto the sys.path Before the import takes place. You can wrap this in a function which backs out the change after the import.
I'm in train so can't test this but that is what I would try.

Related

Where does React's `scryRenderedDOMComponentsWithClass` method name come from?

Working on testing a React component, I was reading the docs and found scryRenderedDOMComponentsWithClass. I'm having trouble understanding the function of this component because it's unpronounceable, so I don't understand how it's naming maps to a mental model of what it's doing. (There are a number of related names, such as scryRenderedDOMComponentsWithTag.)
What does the scry part of this method name refer to? Scary? Scurry? What concept is this name trying to illustrate?
Short answer
"Scry" in this context just means "find all". See this comment on ReactTestUtils.scryRenderedComponentsWithClass. It's a single word, not an abbreviation, and it's pronounced like "cry" but with an "s" at the beginning.
Longer (and nerdier) answer
Elsewhere in that same file, you'll see a reference to DOM.scry:
/**
* Todo: Support the entire DOM.scry query syntax. For now, these simple
* utilities will suffice for testing purposes.
* #lends ReactTestUtils
*/
zpao explains in a comment on a GitHub issue:
That's a reference to an internal Facebook module. It's basically querySelectorAll with fallback behavior for handling old browsers and special cases. It is pretty unremarkable and doesn't actually translate super well here (except maybe a scryRenderedDOMComponentsWithQSA or something, but meh). We're working on improving the testing in other ways so I don't think there's anything we really want to do with this right now.
jimfb takes it a bit further in another GitHub issue, explaining that the name is a reference to Dungeons & Dragons:
Back in the day, we had a bunch of D&D fans on the team.
For reference:
http://www.dandwiki.com/wiki/SRD:Scrying
http://www.dandwiki.com/wiki/SRD3e:Scry_Skill
https://en.wikipedia.org/wiki/Scrying
Historically, we've used scry to indicate a helper that finds a set of results. As the framework matures, we should start choosing function names based on what the functions actually do instead of fantasy words that have very little meaning to the typical developer.
Though I would agree that the word has very little meaning to most, it's worth noting that "scry" is a real English word:
scry
[skrahy]
verb (used without object), scried, scrying.
to use divination to discover hidden knowledge or future events, especially by means of a crystal ball.
Interestingly, according to the data from Google's Ngram Viewer, it seems that the word fell out of normal usage in the early 19th century and then wallowed in obscurity until the 1980s, presumably after D&D gained popularity:
So I can't say I object to jimfb calling it a "fantasy word", especially considering the kind of imagery my imagination conjures up when I hear it.

How to Add to Notepad++ Batch Language or Any Other Ways?

First off I've searched my hind end off for hours now trying to find an answer, but I can't seem to find anything remotely useful. What I am trying to do is to find a way to add in code-folding to the built in batch language. Basically I love using batch, but when I have tons of code, I want to be able to hide the code I do not need to edit which will make it easier to find the code I DO need to edit. What I want is to be able to make it so if I typed "::{" (without quotes) and have finished code in the middle and end with "::}" (also without quotes).
First question, is it possible? Can I add something like this (that one could normally add in the "user defined language") to the built-in batch language?
Next question, if not, where could I figure out how to basically re-create the batch language (and add my own twists) into a new "user defined language"?
Last question, if neither of those are possible, what are my other options?
Like I said, I've researched for hours. I'm not one to ask for help on forums, but I'm desperate at this point. All I want is to use the batch language and have code folding. Doesn't seem like too much to ask, but it might be!
Thanks!
In Notepad++ you can define a language by going to the Language menu --> Define your language (at least in version 6.6.9 anyway). On the Folder & Default tab, under Folding in code 1 style, input a ( into the "Open" box. Input a ) into the "Close" box. Save this as "Windows Batch" (or at least something that doesn't conflict with the in-built language named "batch".
Until you define styles, it'll be ugly and unusable, but it should allow you to collapse / expand parenthetical code blocks as a proof of concept and see whether this project is worthy of further effort. Your next steps will be to copypaste batch keywords from %PROGRAMFILES(x86)%\Notepad++\langs.model.xml, and use the "batch" language styles from your favorite theme in Notepad++\themes\. If I were doing it, I'd input a few basic things using the GUI (like keywords, folding characters, etc.), then export to an XML file on the Desktop and copypaste the rest from a theme, search-&-replacing stuff as needed to massage the theme into your user-defined language. At the end, import your massaged XML into the Define your language dialog. It was going to be more effort than I felt like exerting, but your mileage may vary. If you decide to undertake this journey and you complete it, I hope you'll consider sharing your efforts.
This similar question has a few answers that suggest some workarounds you might find worthwhile -- in particular, hiding, rather than collapsing.

How do I adapt the look and feel of existing Tk apps using TTk?

slebetman says:
But Tk is only really ugly on Unixen because it defaults to a Motif theme (modern Tk is/should be replaced by TTk which is themeable).
Given two Tk apps which I still use occasionally, namely Gitk and ptkdb, how do I change their look and feel so that it matches the rest of the desktop environment (KDE 4)?
See: http://wiki.tcl.tk/gtklook.tcl for a quick way to make default Tk look less painfully ugly. I often use it in my own programs to hide the fact that it's actually written in tcl/tk.
The code given is tcl but you can easily use the options in an Xresources or Xdefaults file which I think should work in other languages as well. Or, it that doesn't work I believe there is an equivalent option method in Perl/Tk*.
Yes, the changes are not much. Basically just reducing pixel widths of things like borders and scrollbars. But it does look much nicer.
*note: I only mention Perl/Tk because you mentioned it in another post. As for modifying gitk, it is written in tcl so you can easily copy-paste the code somewhere.
Check out the TkDocs website, everything I know about modern Tk I learnt from there! Well and experience too of course ;-)
It will not be simple to retrofit it onto a substantial legacy application, but you could probably get surprisingly far by just prefixing the widgets with ttk::.

Django Models / SQLAlchemy are bloated! Any truly Pythonic DB models out there?

"Make things as simple as possible, but no simpler."
Can we find the solution/s that fix the Python database world?
Update: A 'lustdb' prototype has been written by Alex Martelli - if you know any somewhat lightweight, high-level database libraries with multiple backends we could wrap in syntax sugar honey, please weigh in!
from someAmazingDB import *
#we imported a smart model class and db object which talk to database adapter/s
class Task (model):
title = ''
done = False #native types not a custom object we have to think about!
db.taskList = []
#or
db.taskList = expandableTypeCollection(Task) #not sure what this syntax would be
db['taskList'].append(Task(title='Beat old sql interfaces',done=False))
db.taskList.append(Task('Illustrate different syntax modes',True)) # ok maybe we should just use kwargs
#at this point it should be autosaved to a default db option
#by default we should be able to reload the console and access the default db:
>> from someAmazingDB import *
>> print 'Done tasks:'
>> for task in db.taskList:
>> if task.done:
>> print task.title
'Illustrate different syntax modes'
I'm a fan of Python, webPy and Cherry Py, and KISS in general.
We're talking automatic Python to SQL type translation or NoSQL.
We don't have to totally be SQL compatible! Just a scalable subset or ignore it!
Re:model changes, it's ok to ask the developer when they try to change it or have a set of sensible defaults.
Here is the challenge: The above code should work with very little modification or thinking required. Why must we put up with compromise when we know better?
It's 2010, we should be able to code scalable, simple databases in our sleep.
If you think this is important, please upvote!
What you request cannot be done in Python 2.whatever, for a very specific reason. You want to write:
class Task(model):
title = ''
isDone = False
In Python 2.anything, whatever model may possibly be, this cannot ever allow you to predict any "ordering" for the two fields, because the semantics of a class statement are:
execute the body, thus preparing a dict
locate the metaclass and run special methods thereof
Whatever the metaclass may be, step 1 has destroyed any predictability of the fields' order.
Therefore, your desired use of positional parameters, in the snippet:
Task('Illustrate different syntax modes', True)
cannot associate the arguments' values with the model's various fields. (Trying to guess by type association -- hoping no two fields ever have the same type -- would be even more horribly unpythonic than your expressed desire to use db.tasklist and db['tasklist'] indifferently and interchangeably).
One of the backwards-incompatible changes in Python 3 was introduced specifically to deal with situations of this ilk. In Python 3, a custom metaclass can define a __prepare__ function which runs before "step 1" in the above simplified list, and this lets it have more control about the class's body. Specifically, quoting PEP 3115...:
__prepare__ returns a dictionary-like object which is used to store
the class member definitions during evaluation of the class body.
In other words, the class body is evaluated as a function block
(just like it is now), except that the local variables dictionary
is replaced by the dictionary returned from __prepare__. This
dictionary object can be a regular dictionary or a custom mapping
type.
...
An example would be a metaclass that
uses information about the
ordering of member declarations to create a C struct. The metaclass
would provide a custom dictionary that simply keeps a record of the
order of insertions.
You don't want to "create a C struct" as in this example, but the order of fields is crucial (to allow the use of positional parameters that you want) and so the custom metaclass (obtained through base model) would have a __prepare__ classmethod returning an ordered dictionary. This removes the specific issue, but, of course, only if you're willing to switch all of your code using this "magic ORM" to Python 3. Would you be?
Once that's settled, the issue is, what database operations do you want to perform, and how. Your example, of course, does not clarify this at all. Is the taskList attribute name special, or should any other attribute assigned to the db object be "autosaved" (by name and, what other characteristic[s]?) and "autoretrieved" upon use? Are there to be ways to remove entities, alter them, locate them (otherwise than by having once been listed in the same attribute of the db object)? How does your sample code know what DB service to use and how to authenticate to it (e.g. by userid and password) if it requires authentication?
The specific tasks you list would not be hard to implement (e.g. on top of Google App Engine's storage service, which does not require authentication nor specification of "what DB service to use"). model's metaclass would introspect the class's fields and generate a GAE Model for the class, the db object would use __setattr__ to set an atexit trigger for storing the final value of an attribute (as an entity in a different kind of Model of course), and __getattr__ to fetch that attribute's info back from storage. Of course without some extra database functionality this all would be pretty useless;-).
Edit: so I did a little prototype (Python 2.6, and based on sqlite) and put it up on http://www.aleax.it/lustdb.zip -- it's a 3K zipfile including 225-lines lustdb.py (too long to post here) and two small test files roughly equivalent to the OP's originals: test0.py is...:
from lustdb import *
class Task(Model):
title = ''
done = False
db.taskList = []
db.taskList.append(Task(title='Beat old sql interfaces', done=False))
db.taskList.append(Task(title='Illustrate different syntax modes', done=True))
and test1.p1 is...:
from lustdb import *
print 'Done tasks:'
for task in db.taskList:
if task.done:
print task
Running test0.py (on a machine with a writable /tmp directory -- i.e., any Unix-y OS, or, on Windows, one on which a mkdir \tmp has been run at any previous time;-) has no output; after that, running test1.py outputs:
Done tasks:
Task(done=True, title=u'Illustrate different syntax modes')
Note that these are vastly less "crazily magical" than the OP's examples, in many ways, such as...:
1. no (expletive delete) redundancy whereby `db.taskList` is a synonym of `db['taskList']`, only the sensible former syntax (attribute-access) is supported
2. no mysterious (and totally crazy) way whereby a `done` attribute magically becomes `isDone` instead midway through the code
3. no mysterious (and utterly batty) way whereby a `print task` arbitrarily (or magically?) picks and prints just one of the attributes of the task
4. no weird gyrations and incantations to allow positional-attributes in lieu of named ones (this one the OP agreed to)
The prototype of course (as prototypes will;-) leaves a lot to be desired in many respects (clarity, documentation, unit tests, optimization, error checking and diagnosis, portability among different back-ends, and especially DB features beyond those implied in the question). The missing DB features are legion (for example, the OP's original examples give no way to identify a "primary key" for a model, or any other kinds of uniqueness constraints, so duplicates can abound; and it only gets worse from there;-). Nevertheless, for 225 lines (190 net of empty lines, comments and docstrings;-), it's not too bad in my biased opinion.
The proper way to continue playing with this project would of course be to initiate a new lustdb open source project on the hosting part of code.google.com (or any other good open source hosting site with issue tracker, wiki, code reviews support, online browsing, DVCS support, etc, etc) - I'd do it myself but I'm close to the limit in terms of number of open source projects I can initiate on code.google.com and don't want to "burn" the last one or two in this way;-).
BTW, the lustdb name for the module is a play of word with the OP's initials (first two letters each of first and last names), in the tradition of awk and friends -- I think it sounds nicely (and most other obvious names such as simpledb and dumbdb are taken;-).
I think you should try ZODB. It is object oriented database designed for storing python objects. Its API is quite close to example you have provided in your question, just take a look at tutorial.
What about using Elixir?
Forget ORM! I like vanilla SQL. The python wrappers like psycopg2 for postgreSQL do automatic type conversion, offer pretty good protection against SQL injection, and are nice and simple.
sql = "SELECT * FROM table WHERE id=%s"
data = (5,)
cursor.execute(sql, data)
The more I think on't the more the Smalltalk model of operation seems more relevant. Indeed the OP may not have reached far enough by using the term "database" to describe a thing which should have no need for naming.
A running Python interpreter has a pile of objects that live in memory. Their inter-relationships can be arbitrarily complex, but namespaces and the "tags" that objects are bound to are very flexible. And as pickle can explicitly serialize arbitrary structures for persistence, it doesn't seem that much of a reach to consider each Python interpreter living in that object space. Why should that object space evaporate with the interpreter's close? Semantically, this could be viewed as an extension of the anydbm tied dictionaries. And since most every thing in Python is dictionary-like, the mechanism is almost already there.
I think this may be the generic model that Alex Martelli was proposing above, it might be nice to say something like:
class Book:
def __init__(self, attributes):
self.attributes = attributes
def __getattr__(....): pass
$ python
>>> import book
>>> my_stuff.library = {'garp':
Book({'author': 'John Irving', 'title': 'The World According to Garp',
'isbn': '0-525-23770-4', 'location': 'kitchen table',
'bookmark': 'page 127'}),
...
}
>>> exit
[sometime next week]
$ python
>>> import my_stuff
>>> print my_stuff.library['garp'].location
'kitchen table'
# or even
>>> for book in my_stuff.library where book.location.contains('kitchen'):
print book.title
I don't know that you'd call the resultant language Python, but it seems like it is not that hard to implement and makes backing store equivalent to active store.
There is a natural tension between the inherent structure imposed - and sometimes desired - by RDBMs and the rather free-form navel-gazing put here, but NoSQLy databases are already approaching the content addressable memory model and probably better approximates how our minds keep track of things. Contrariwise, you wouldn't want to keep all the corporate purchase orders such a storage system, but perhaps you might.
How about you give an example of how "simple" you want your "dealing with database" to be, and I then tell you all the stuff that is needed for that "simplicity" to get working ?
(And of which it will still be YOU that will be required to give the information/config to the database interface engine, somewhere, somehow.)
To name but one example :
If your database management engine is some external machine with which you/your app interfaces over IP or some such, there is no way around the fact that the IP identity of where that database engine is running, will have to be provided by your app's database interface client, somewhere, somehow. Regardless of whether that gets explicitly exposed in the code or not.
I've been busy, here it is, released under LGPL:
http://github.com/lukestanley/lustdb
It uses JSON as it's backend at the moment.
This is not the same codebase Alex Martelli did.
I wanted to make the code more readable and reusable with different
backends and such.
Elsewhere I have been working on object oriented HTML elements
accessable in Python in similar ways, AND a library for making web.py
more minimalist.
I'm thinking of ways of using all 3 elements together with automatic
MVC prototype construction or smart mapping.
While old fashioned text based template web programming will be around
for a while still because of legacy systems and because it doesn't
require any particular library or implementation, I feel soon we'll
have a lot more efficent ways of building robust, prototype friendly
web apps.
Please see the mailing list for those interested.
If you like CherryPy, you might like the complementary ORMs I wrote: GeniuSQL (which follows a Table Data gateway model) and Dejavu (which is a complete Data Mapper).
There's far too much in this question and all its subcomments to address completely, but one thing I wanted to point out was that GeniuSQL and Dejavu have a very robust system for mapping native Python types to the types that your particular backend is using. There are very sane defaults, which can be overridden as needed, and even extended if you make a new backend or use types from a backend that isn't yet supported. See http://www.aminus.net/geniusql/chrome/common/doc/trunk/advanced.html#custom for more discussion on that.

If I wanted to define a file format, how would I go about that?

Say I come up with some super-duper way of representing some data that I think would be useful for other people to know about and use. Assume I have a 'spec' in some form, even if it might not be a completely formal one: ie, I know how this file format will work already.
How would I then go about releasing this spec to get comments and feedback based on it? How would I get it 'standardised' in some form?
Specifying file formats is difficult. If the data you want to store is trivial, it tends to be trivial. In general however, this is hardly the case. You can use the RFC structure and keywords, but I always found specifying a fileformat in prose a slow, difficult and boring task, also because reading it is likewise difficult.
My suggestion, if you want to follow this way, is to focus on blocks of information. Most of the difficuly is for entities that are optional, and present only if another condition happens, so try to exploit this when partitioning your data.
The best spec, IMHO, is real code with an uberperfect testsuite.
As for standardization, if enough people use it, it becomes a de-facto standard. you don't need an official stamp for it, although when the format is used enough, you could benefit from an official mime type.
To talk about it, well, it depends. I found useful to talk in terms of "object oriented" entities, and also in terms of relationships. Database-like diagrams are very useful on this respect.
Finally, try to find a decent already standard alternative first, or at least try not to deal with the raw bits. There are a lot of perfect container formats out there that free you of many annoying tasks. The choice of the container depends on the actual type of file format (e.g. if you need encryption, interleaving, transactions, etc).
There are a couple of ways I'd go about it, I think.
First, determine if there's a standards body (like W3C, or IEEE) that might be related to your file format. If there is, pitch it to them. I have no idea how receptive they'd be though.
Second, a standard is useless if nobody is using it. Get some momentum behind it. Write a blog post, twitter and make a website about it. Link on programming.reddit.com, and slashdot. Describe it to your friends and colleagues. Post it here on SO, and ask for feedback.
HTH.

Resources