Python is a dynamically typed, high-level interpreted programming 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 and exceptions, and has an extensive standard module library. ...

learn more… | top users | synonyms (1)

2
votes
1answer
169 views

Type checking as opposed to multiple functions?

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know): public String flip(String text) { String result = ""; for (int i = ...
0
votes
1answer
184 views

How to Handle Duplicate Web Requests on Server

I'm running a Twisted web server and trying to figure out a good way to handle retries from the client. In normal operation the client makes a request of the server with some JSON data and receives a ...
5
votes
1answer
122 views

How to organize my module

This is currently the way I'm organizing it. myProject/ README subproject1/ __init__.py classA.py classB.py driver.py basicTest.py Driver imports ...
4
votes
2answers
1k views

Exception handling in Python - Am I doing this wrong (and why?)

I've read many questions and articles on exception handling in Python (and in general), but I still think that it's the most confusing thing ever. I ended up doing something like this: # error class ...
1
vote
1answer
73 views

I am able to use PUT to act like GET when using my REST API

I wrote a simple REST API (just learning) using Go and I am calling it using python requests. I have two methods, "update_x", which updates the value of the resource, "get_x_times_n" which gets the ...
1
vote
2answers
272 views

How to achieve inheritance when using just modules and vanilla functions in Python?

Python doesn't enforce any paradigm. It gives you freedom. Python provides encapsulation at module level. If I have a module A and have a module B with same interface. How do I sort of inherit B ...
1
vote
0answers
77 views

How to re-use a very small Python utility across different applications?

I have a very small utility that has proven itself very handy across a multitude of applications. The utility itself is less than 50 lines of code and is its own Python module, and a number of ...
1
vote
1answer
312 views

Hard time understanding structure of web apps w JS frameworks? [closed]

I'm having a difficult time understanding how web applications work. Please help me understand without giving me google definitions. Baby steps/words would be appreciated =) From what I understand so ...
3
votes
3answers
79 views

Premature memoization on obvious cases

In the code below there will be several instances of MainClass. class MainClass(object): def f(self, x): # expensive operations. # ... return 'something' Each instance ...
0
votes
2answers
182 views

A Python program that illustrates the tree-like structure of recursion [closed]

I want to write a program in Python that illustrates the tree-like feature of recursion, using the example of "fibo(n)" function. I wonder how to modify the following program: def fibo(n): if n==...
38
votes
6answers
3k views

Should I create a class if my function is complex and has a lot of variables?

This question is somewhat language-agnostic, but not completely, since Object Oriented Programming (OOP) is different in, for example, Java, which doesn't have first-class functions, than it is in ...
0
votes
0answers
17 views

How do I write a loader library for a Python-based KIO slave

I'm trying my hand at writing a KIO slave and chose python because, well, that's what I know. There's even a decent example here Then here it tells you that for Python you need a C loader library ...
2
votes
0answers
78 views

Reducing conditional code makes assessing coverage or debugging difficult

Working in Python, I've written a few utilities to assert pre-conditions and to re-interpret exceptions, and have found my code doesn't have so many branches. I've also tried to move other code path ...
4
votes
2answers
218 views

How to handle singular and plural parameters for functions

Often when I'm writing a set of utility functions, I find myself wanting a function that applies some logic to multiple items, as well as a function that does the same with just a single item. There ...
5
votes
4answers
295 views

Shell commands in bash or python? How much encapsulation is too much?

I'm thinking about how to decide whether it's better to encapsulate my work behind well-named function names, or to expose it - which will help developers understand what's going on more quickly? Is ...
1
vote
1answer
102 views

How do I refactor a loop that does aggregation while doing it's regular job ?

What am I suppose to do I am working on an API, where I get a list of objects from the output of an SQL query using an ORM. Now this API call needs to send following information down to the client. ...
0
votes
2answers
145 views

Why does presence of value evaluate as “Truthy” in Python? [e.i. Types evaluate True in conditionals]

Example: if float: print('float is true?') returns: Why do all objects other than ones explicitly defined as False or "False" evaluate truthfully?
2
votes
2answers
223 views

writing a controller file in Python

I need some advice on my idea to write a controller file in Python, and not like the C in MVC type controller but a more simpler idea. Just a Python script that controls the operation of some other ...
2
votes
2answers
293 views

Is it OK to use (WTF) forms to validate against stuff from DB?

Form classes are intended (IMO) for submitted data against rules. For example: are passwords equal, is end date later than start date. submitted data--->|Form| Is it okay for Form classes to ...
-1
votes
2answers
351 views

How are complex programs made? [duplicate]

I know a few languages, and can program in them. How do multiple languages blend together in a single program? For ex. https://github.com/facebook/watchman. This uses C, PHP, Python, Javascript, etc. ...
0
votes
2answers
203 views

Ensuring program output is always the same [closed]

I am working on a +10k LOC program, and I need to ensure its output is always the same for given input. The program consists of dozens of modules and classes, inherited by a MainClass. (The examples ...
9
votes
2answers
266 views

Is **kwargs an antipattern?

We have a lot of code in our internal codebase that calls our libraries internally - these libraries often have a lot of arguments (think matplotlib) and our code is often doing only a specific task ...
0
votes
0answers
90 views

Reusing models and classes with blueprints with flask

Designing a content management system using Python/Flask. I have a blueprint module called pages [app/pages/..]. Pages contains the title, description and content. It can be queried using /pages/&...
0
votes
0answers
16 views

How much additional specialised functionality should I bundle with my main flask application?

I am building a custom content management system (i.e. html, some js) for a project using Flask/Python. As things have progressed, I have seen that I need to add: Some AJAX for interactivity. ...
1
vote
2answers
113 views

Is it considered good practice to use mutables this way? [closed]

Lately I came across a way of using mutables which I had not thought of before. Let's say you have a list comprehension which calls a method, and you put the result in a list like the following (...
4
votes
2answers
917 views

Why does Python's math.ceil return a float?

While I understand that the difference between integral and long values is blurred in Python, the difference between floats and integral values is not. Therefore, I'm having a difficult time ...
6
votes
1answer
228 views

UI Design patterns for non-screen based project

I'm working on a small project coded in Python that uses a single button for input and a RGB LED as feedback, which responds to button presses and asynchronous events from the network. As the ...
1
vote
1answer
113 views

Is Python's calendar.month_name implementation of an empty string in the first index good practice or poor design?

The documentation states: An array that represents the months of the year in the current locale. This follows normal convention of January being month number 1, so it has a length of 13 and ...
2
votes
1answer
57 views

Should I be taking these as arguments or define them in a sub class?

So I am making a mario clone in pygame and I have a base class Character and two sub classes, Mario and Luigi. The methods that the Character class defines require a significant amount of attributes ...
4
votes
2answers
252 views

Extract name of return variable from Python function

I have a python function x that returns some module x(): return y I would like to parse the file x is located in and learn that "x returns this thing called 'y'". I have no idea how. I'm not trying ...
1
vote
0answers
83 views

How can I improve my websocket server's structure/flow?

I'm building a websocket server in python and I've hit a block regarding my program's flow and structure. Here's what I'm doing at the moment. The program creates a instance of 'server' and listens ...
0
votes
1answer
40 views

Ways of embedding static data with python application

I have several projects on Python I am currently versioning with Git. I also have the input-output data that is gradually changing over the course of the project. I need to be able to re-run my ...
2
votes
1answer
224 views

Patterns for sharing context variables between functions

I am looking for ways of passing around a set of contextual variables that are required by functions. As a Python programmer, right now I can see three ways of solving the problem: passing them ...
1
vote
2answers
92 views

Factored out loop control in Python

I have a for loop in python like this: for item in items: onwards = make_flow_decision(item) if onwards == 'break': break elif onwards == 'continue': continue elif ...
8
votes
3answers
341 views

Is the benefit of the IO monad pattern for handling side effects purely academic?

Sorry for yet another FP + side effects question, but I couldn't find an existing one which quite answered this for me. My (limited) understanding of functional programming is that state/side effects ...
1
vote
2answers
225 views

How should I structure these Python classes?

Base Class I have a class called Remote. This class represents a remote machine and has properties such as ip, hostname, username, and password, as well as methods for transferring files to/from the ...
0
votes
1answer
92 views

How to properly handle indefinite generators in Python

Let's say we have a generator that is indefinite, where new elements can arrive at any moment with significant (up to indefinite) delay. An example of such generator is tail -F command. In python (...
0
votes
2answers
95 views

Is there an algorithm to avoid getting in hundreds of datapoints from API to represent a bar graph?

I am showing a graph on HTML5 canvas using the flot charting library for a between a start date and an end date. It's fine as long as the period is less than a year or so but if the user selects a big ...
4
votes
2answers
1k views

Python classes with only one instance: When to create a (single) class instance and when to work with the class instead?

Given a Python class which will be instantiated only once, i.e. there will be only one object of the class. I was wondering in which cases it makes sense to create a single class instance instead of ...
4
votes
3answers
821 views

Is this OOP class hierarchy too deep?

So I am making a Super Mario Bros NES clone in pygame and for all enemies that are drawn to the window, I have a class hierarchy that is 5 classes deep. Going in the order of: Object (A base class ...
1
vote
0answers
59 views

Writing a container that treats nearly-identical images as the same

I want to create a mapping in Python that will use images as keys, but treat similar (but not identical) images as the same key. I have an approach for deciding which images are similar enough: I ...
1
vote
1answer
42 views

How to represent alternative and sequential tasks?

I am experimenting with hierarchical task planning (in python) and I would like to have functions which return lists of tasks. I need to differentiate between alternative paths and sequential tasks. ...
2
votes
1answer
237 views

Is there a better algorithm to distribute integer to X integers minimizing their difference?

Is there a better algorithm to distribute values from one source to X destinations minimizing their difference? I have some source integer. I need to know how much of that value I need to distribute ...
3
votes
1answer
262 views

Structuring a Python Project: Need some guidance

I currently am working on a project that involves several discrete components that have some common functionality, and I am in need of some advice. Right now I have a central component responsible ...
0
votes
1answer
163 views

Optional text qualifier in CSV file as input to SSIS package

I receive several CSV files each quarter to process through an SSIS package, and the formatting is inconsistent. This quarter, I encountered what I term optional text qualifiers--text qualifer of ...
0
votes
1answer
107 views

Should I put the parameters in constructor or in method? (Python 3)

I have the following code: def __init__(self, vocable_file_path, xsd_file_path, word_list_file_path): self.vocable_file_path = vocable_file_path self.xsd_file_path = xsd_file_path self....
1
vote
2answers
165 views

Is there a better pattern than multiple inheritance here?

I'm working on a server implementation for a large game with many gametypes. There are several kinds of interactable entities: players, monsters, objects, vehicles. All entities share the same base ...
1
vote
1answer
252 views

Could there be a use case for C# style auto-properties in Python

I've been doing a lot of work in C# recently (my primary language is Python) and I love that I can do something like public string MyProperty{ get; set; } Going back to Python I miss that sort of ...
2
votes
2answers
205 views

Final steps of wrapping up a Python script into a program

I finished building a Python script containing eight functions which download files, extracts them, manipulate and analyze their data and then produce graphs and export them into PNG files. The ...
0
votes
1answer
148 views

Can I listen on a port without interrupting traffic?

I have developed the bare bones on a socket server in Python. I'm trying to capture the HTTP packets going to a web server without blocking them. try: self.socket = socket.socket(socket....