Python is a dynamic, high-level language. Its design focuses on clear syntax, an intuitive approach to object-oriented programming, and making the right way to do things obvious. Python supports modules, exceptions, had an extensive standard module library. Python can also be embedded in other ...

learn more… | top users | synonyms (1)

1
vote
2answers
79 views

Is it safe to catch ImportError when trying to import optional modules?

I usually see this pattern at least once in every Python project I work on. For example, in a Django project, this is often added at the bottom of the base settings file: try: from .local_settings ...
0
votes
3answers
133 views

Socket Connecting to Large number of IPs

I have a text file of ~600 CIDR notation IP blocks which, when expanded, amount to ~17.5M IP addresses. I need to socket connect to each one. If it connects, I add it to a "live" list, if it returns ...
4
votes
2answers
139 views

In Python, is there any difference (apart from a few listed exceptions) between classes and dictionaries?

My logic goes like this: def A(val): return {'a':val} print(A(7)['a']) is the same as class A: def __init__(self, val): self.a = val print(A(7).a) Obviously, there are problems ...
16
votes
3answers
962 views

Should I pass in filenames to be opened, or open files?

Suppose I have a function that does things with a text file - for example reads from it and removes the word 'a'. I could either pass it a filename and handle the opening/closing in the function, or ...
0
votes
2answers
85 views

Print Statements Inside Function Or Before Calling It? [closed]

I have a script which different people may use. I have print statements so people can follow along what the script is doing, and if it breaks where it went wrong, and if a certain step takes a long ...
1
vote
0answers
17 views

How should private dependency links be handled in setup.py

At work we use a private pypi server. This pypi server is specified as a dependency link: ... from setuptools import setup config = ConfigParser.ConfigParser() rc = ...
0
votes
1answer
39 views

using one database connection across multiple functions in python

whats the best way to go about this in python? I have a number of like so def test1() .... code ..... def test2() .... code ..... def test3() .... code ..... how could I create one ...
0
votes
3answers
117 views

OOP: relying on protected properties in a class-subclass contract

I'm coding in Python, but the question seems independent of programming language. I have a class that represents some system check: class Check: @abstractmethod def run() """ You have to ...
1
vote
1answer
71 views

graph implementation in PYthon

I have found the following implementation of a Flow algorithm in Python: class Edge(object): def __init__(self, u, v, w): self.source = u self.sink = v self.capacity = w ...
0
votes
1answer
78 views

Is there a canonical way to cache instance methods in python?

I have some computationally intensive functions in my python script that I would like to cache. I went looking for solutions on stack overflow and found lots of links: ...
-3
votes
1answer
69 views

Understanding searching through dicts in python [closed]

I'm trying to understand the differences between dict, dict.items and dict.iteritems. I know that items returns a list of all the items, while iteritems returns an iterator, which for large lists can ...
0
votes
1answer
51 views

No repeated coordinates in random walker ensemble

i'm simulating an ensemble of random walkers in 2D in python, I set [x,y] coordinates of my walkers to be gaussian in a 2D grid, then use a rand array of -1 and +1 to move up down left right: N=20 ...
2
votes
2answers
127 views

How much should I break up my unit tests?

I recently learned about unit tests and TDD and have started going back to add unit tests to my code (and thank god, it's proven that some things I've written are much more broken than I thought). ...
1
vote
1answer
201 views

Should I couple these classes?

I am programming in Python. I have several complicated/hard-to-understand XML files that describe the structure of an election. I am trying to write a python wrapper that makes it easy to access this ...
1
vote
1answer
87 views

Transferring information between a web server and a Python backend?

I have been Googling my problem but haven't been able to come up with anything; here's the situation: I have a PHP / MySQL website hosted with 1and1.com. I also have a Python program that runs on a ...
0
votes
1answer
32 views

Plugins and exporting an API between python scripts

I've got a python script, which should read a subdirectory and make all the scripts within it callable like this: python mainscript.py subscript arguments I've figured out most of the details but ...
1
vote
2answers
173 views

How-To: a browser based, real time, shared memory inspection/debugger application [closed]

Background: I have a C application running under Linux utilizing shared memory. I also have a debugger written in Python that can link to said shared memory and access individual variable values of ...
2
votes
0answers
34 views

Breaking a table over multiple lines in Sphinx

Lets say I have the following function with a docstring: def foo(*args): """ do some fooness +----------------------------+----------------------------------------------------+ | ...
2
votes
1answer
138 views

What exactly is this Python statement doing?

I'm trying to port a very basic dual contouring algorithm written in Python (here) to C++. I'm not a Python programmer so some of the language syntax is a bit foreign to me. Specifically, this block ...
-3
votes
2answers
49 views

How to perform deferred tasks in backend [closed]

Actually the problem is very common. Let me describe this by example. Imagine you have a web service where users register and pay for some gold status. There is expiration date. The question is how to ...
1
vote
4answers
157 views

How to clean up nested try/except/else?

When writing code, I often want to do something like this: try: foo() except FooError: handle_foo() else: try: bar() except BarError: handle_bar() else: ...
3
votes
1answer
98 views

Seeking Guidance on Thread Safe Scripting

I am trying to write a script that will take multiple paths to files on various servers, search them all simultaneously, and return a single list of results to a user. Initially, I was just using ...
0
votes
0answers
34 views

Advice on which approach to follow for sandboxing the input in an intentional “vulnerable” web app (sandbox in python) [duplicate]

my question is about which is the best approach/practice on building a "sandbox mechanism" in Python in a web application. The app will provide intentional vulnerable scenarios, and the user has to ...
4
votes
3answers
522 views

How to avoid large function arity / exposure of underlying implementation

Currently reading Clean Code, and the authors seem to imply that any function with an arity greater than 3 should be avoided at all costs. I am unfortunately running into an issue where I am faced ...
0
votes
2answers
99 views

Prime symbol in Python variable name

So I'm a terrible person and I want to name a variable in my mathy-python3 code s′ (that's a prime symbol). I was under the impression Unicode literals work as identifiers in Python 3, which is why ...
1
vote
1answer
51 views

Is there a cleaner way to test extras_require

I have code reviewed a piece of Python code, but to me it looks really ugly, hacky and complex for something that can be achieved very easily. The code looks something similar to the following: ...
1
vote
2answers
106 views

Function has two sets of arguments, the latter of which calls a nested function? [closed]

The function foo, has a nested function; bar. Calling foo with a second set of arguments feeds them into bar if bar is returned at the end of foo. How does this work and what is it called? Are there ...
0
votes
1answer
175 views

Is there a way to prevent variables from changing their type in Python?

It is useful to have the interpreter derive the type of a variable automatically. This on its own is similar to the auto keyword in C++11. However, in Python variables can change their type after ...
0
votes
0answers
67 views

Functional dependencies diagram

I'm searching for the correct type of diagram in which I can see all dependencies between the functions, classes and files of my Python program (multiple files). It's for cleaning purposes. So my ...
-2
votes
1answer
204 views

Base Class, with method that returns Derived Class. design flaw?

I'm writing a Math equation solver in python (for fun). The first design that came to my mind: Expression: base class for every item in an equation (maybe bad name). BinaryExpression extends ...
3
votes
3answers
181 views

Lambda expressions with no parameters in Haskell and / or lambda calculus

In eager languages like Scheme and Python, you can use a lambda expression without parameters to delay evaluation, e.g. in Scheme (Chicken Scheme): #;1> (define (make-thunk x) (lambda () (+ x 1))) ...
0
votes
1answer
68 views

Should instance variables be bound to the instance right in the beginning of the method they are created in?

A common problem for me - and it's not really a problem, but somehow I again and again ask myself, what is the normal way to handle this - is that there is a method and within this method a couple of ...
2
votes
2answers
168 views

Building a string parser for user command and control?

My goal is to build a command parser that has basic syntax and multiple possible branches at each point. These commands come from users of the system and are text input (no GUI). The basic syntax is ...
2
votes
1answer
62 views

Python property best practices [closed]

Is there any situation where it makes sense to define a python property, where getting (not setting) it would raise an exception? I don't think this ever happens for fields in classes, hence the ...
0
votes
3answers
152 views

Is this a pattern: stand-alone classes

Having recently discovered python, I attempted to write a simple logger. Data is read from a device, processes, displayed and stored on disc. Those different tasks belong to different modules, of ...
1
vote
1answer
78 views

writing functional/integration tests for python

I am new to python and functional/integration tests as a whole. I know how to write unit tests, but in this case i do not need isolation for specific functions, rather i need my python script to be ...
3
votes
2answers
147 views

Is a bad practice create database structure on the runtime?

Im creating a crawler app that will always in the beggining, when the Class constructor, ensure that the basic database structure exists. Is this a bad practice? What is the advantage os creating the ...
-1
votes
1answer
111 views

Unevenly distributed random number generation [duplicate]

Let's say I have to generate random number from 1 to 100, however, the probability of each number is not 1/100, but a predefined probability. How to generate that number? I use Ruby/Python.
1
vote
4answers
119 views

Efficient “Object with weights” structure

I need to find an "efficient" data structure / set of algorithms to do the following things: I have a list of objects. I need to assign them weights and later on increase or decrease these weights. ...
0
votes
1answer
124 views

Simple unicode application?

I want to create simple language learning applications to help friends in learning languages. A simple Java console application would do the trick, but the Windows console does not seem to handle ...
0
votes
2answers
131 views

At what point should you start putting functions in a new file? [closed]

I'm currently writing a python script. It's currently about 400 lines long, and it seems like it's getting a bit long, as when I'm trying to scroll through it with eclipse it takes a bit to get from ...
0
votes
1answer
124 views

Should an object query its owner?

I have a World class World: def __init__(self): self.size = 10 self.n_busStops = 10 self.busStopMatrix = SpatialGraph(self.size, self.n_busStops, 1.0) self.buses ...
4
votes
6answers
233 views

Should I store x,y coordinates as an array, a class object, or two variables?

I have a MyObject which has an x and y coordinate. as far as I can see, I can store it in three ways: class MyObject: def __init__(self, x, y): self.x = x self.y = y class ...
13
votes
5answers
2k views

Is it a good practice to declare instance variables as None in a class in Python?

Consider the following class: class Person: def __init__(self, name, age): self.name = name self.age = age My coworkers tend to define it like this: class Person: name = ...
1
vote
2answers
148 views

Is creating and writing to one large file faster than creating and writing to many smaller files in Python on Linux?

If using Python on a Linux machine, which of the following would be faster? Why? Creating a file at the very beginning of the program, writing very large amounts of data (text), closing it, then ...
36
votes
2answers
4k views

How do I move away from the “for-loop” school of thought?

This is a rather conceptual question, but I was hoping I could get some good advice on this. A lot of the programming I do is with (NumPy) arrays; I often have to match items in two or more arrays ...
3
votes
2answers
206 views

Which programming idiom to choose for this open source library?

I have an interesting question about which programming idiom is easier to use for beginner developers writing concrete file parsing classes. I'm developing an open source library, which one of the ...
2
votes
2answers
791 views

Why doesn't Python have a “flatten” function for lists?

Erlang and Ruby both come with functions for flattening arrays. It seems like such a simple and useful tool to add to a language. One could do this: >>> mess = [[1, [2]], 3, [[[4, 5]], 6]] ...
4
votes
4answers
706 views

How does Python compile some its code in C?

I read that some constructs of Python are more efficient because they are compiled in C. https://wiki.python.org/moin/PythonSpeed/PerformanceTips Some of the examples used were map() and filter(). I ...
3
votes
1answer
157 views

What is the right way to process inconsistent data files?

I'm working at a company that uses Excel files to store product data, specifically, test results from products before they are shipped out. There are a few thousand spreadsheets with anywhere from ...