Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 40543

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Use the python tag for all Python related questions. If you believe your question may be even more specific, you can include a version specific tag such as python-3.x.

7 votes

A source Code Counter

Instead of having to update the number of lines each iteration through the file, you could use the enumerate function and only assign the line number once: with open('test_file.txt', 'r') as f: f …
BeetDemGuise's user avatar
  • 4,226
2 votes
Accepted

Dynamically assign values

Your question was a little confusing. But I will try and summarize it before I go into my solution: You have a struct that you are unpacking and during that process you get the number of IPs 'they' …
BeetDemGuise's user avatar
  • 4,226
3 votes
Accepted

Determine whether a list contains a particular sequence

Instead of writing your own logic, Python has a built-in library difflib that contains the SequenceMatcher class. … First of all, take a look at PEP8, the official Python style guide. It gives a great reference to how your code should look. …
BeetDemGuise's user avatar
  • 4,226
4 votes
Accepted

Soccer winning probabilies from ELO strength indicator

There is also no need to specify your operands as floats (400.0 vs 400) because Python does float division with the basic / operator. …
BeetDemGuise's user avatar
  • 4,226
17 votes

Splitting a list by indexes

There is a lot simpler way to do this. You can use list slicing and the zip function. List slicing essentially cuts up a given list into sections. The general form is list[start:stop:step]. The start …
BeetDemGuise's user avatar
  • 4,226
3 votes

Simple Caesar shift and deshift

Take a look at PEP8, the official Python style guide. This guide will help you style your code so that it looks and feels more Pythonic. One general note, the input function returns a string. … The string type in Python is iterable, which means that it can used in a list comprehension: list_of_characters = [char for char in 'hello world!'] …
BeetDemGuise's user avatar
  • 4,226
5 votes

Python function to find and tokenize string data

If you have more concerns about style, consult PEP8, the official Python style guide. There is a flaw in your logic though. …
BeetDemGuise's user avatar
  • 4,226
2 votes
Accepted

Observer pattern implementation without subclassing

You could just use id but that may be a little confusing (and possibly dangerous) since id is a basic Python function. My final point about variable names deals with multiple word names. …
BeetDemGuise's user avatar
  • 4,226
2 votes
Accepted

File validation and unit testing

As a general statement, Python convention uses underscores_in_names instead of camelCase. Class Structure This implementation is really up to you. …
BeetDemGuise's user avatar
  • 4,226
3 votes

Searching a dictionary of words using difflib

They are descriptive (for the most part) but the official Python style guide recommends using underscores_between_words instead of camelCaseNames. Same basic idea for module names. …
BeetDemGuise's user avatar
  • 4,226
3 votes
Accepted

Implement operation and simulation mode in an interface

_mode = mode # To be continued Each function in SimulationDevice and OperationDevice needs to be named differently because of how Python interprets and resolves inheritence. …
BeetDemGuise's user avatar
  • 4,226
3 votes
Accepted

Check ordinary least squares calculation for correctness

Here are a few pointers: Start function/method names with a verb. So I would change mean and variance to get_mean and get_variance respectively. Your return statements in mean and variance can be pu …
BeetDemGuise's user avatar
  • 4,226
4 votes
Accepted

Simplifying if else statements in Python code snippet

Recursion here is going to be your friend: def recurse_json(property, variant): # This is our exit condition if isinstance(property, dict): json_key_getter(property, variant) else …
BeetDemGuise's user avatar
  • 4,226
2 votes

Call method of specific parent class

You could map the individual parent to a specific mode in you maps as such: class DualModeDevice(OperationDevice, SimulationDevice): def __init__(self, mode='simulation'): super(DualModeD …
BeetDemGuise's user avatar
  • 4,226
11 votes
Accepted

Configuration file with Python functionality

However, conventional Python uses a 4-space indentation level to mark deeper blocks of code. In Python, the naming convention for basically everything is to use underscores_in_names. … However, in Python 3.X this actually errors. Essentially what you want to do is get pairs of consecutive elements in an iterable. …
BeetDemGuise's user avatar
  • 4,226

1
2 3 4 5
15 30 50 per page