Questions tagged [python-2.x]

Python 2 is the predecessor of Python 3 and its last version, Python 2.7 was formally released on July 3, 2010. Use this tag along with the main python tag to denote programs that are meant to be run on a Python 2 interpreter only. Do not mix this tag with the python-3.x tag.

Filter by
Sorted by
Tagged with
19 votes
5 answers
60k views

Get value from dictionary given a list of nested keys

I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
7 votes
1 answer
556 views

Hit the target number

After I played the game "The Mesh" for a while I figured that I wanted to make a solver in Python. See the video on the website for a quick intro, I also explain the rules below. The Mesh ...
3 votes
2 answers
97 views

JSON Lexer code

How does the following look for a json lexer? Are there any things that can be improved upon? Does the pattern look general-purpose enough to be able to generalize it to make it into a useful program, ...
2 votes
1 answer
3k views

Convert letters into numbers (A=1, B=2 etc)

...
7 votes
1 answer
104 views

Zalera calculator

I recently started replaying Final Fantasy XII. One of its optional bosses, Zalera, has, among others, the following mechanics: His attack sequence is Lv. 2 Sleep, Lv. 3 Disable, Lv. 4 Break and Lv. ...
2 votes
1 answer
15k views

Text-based Python RPG game

This is a little text-based Python adventure game I found in one of my old folders. This one has a battle system, a shop, a save option and more. You can fight creatures for gold, and so much more! <...
7 votes
4 answers
6k views

Find the shortest whole repetitive substring

I'm working on a problem to find wholly repeated shortest substring of a given string, and if no match, return length of the string. My major idea is using a Trie ...
5 votes
1 answer
178 views

Python Project Template

What's up smart people. A little backstory for context: I'm a completely self taught dev, and the only person ever looking at my code for the past 6 years is myself. I realize that's not the best idea ...
7 votes
1 answer
155 views

Scrape points from CTF sites

I am relatively new to classes. This one uses one. I am pretty sure this is not the correct way to do this. But I also do not know the correct way. You can use the ...
0 votes
1 answer
100 views

Function to add a new key pair value to my JSON file

As you can see I am using a counter variable to iterate through my JSON file and it's a dictionary that has a list of dictionaries. Once I find the key normalHours I divide it and add it to my ...
1 vote
1 answer
97 views

Register/Login for Rock/Paper/Scissors

Recently I have been learning Python after years of putting it off, and I am having a really fun time with it. However after I spent a few hours making something that I thought was pretty cool, I ...
4 votes
2 answers
550 views

TapeEquilibrium Codility implementation not achieving 100%

Given the following task description from here : A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, such that 0 < P &...
7 votes
2 answers
1k views

Repeatable HTTP requests in Python

I have a simple python script that's repeating a HTTP response/request. I would like to know how I could have constructed this better, particularly if there's a way I could have incorporated the slice ...
3 votes
1 answer
3k views

Downloading files using Python-requests

I wrote a Python script to download files using multiple (source) IP addresses -- kindly suggest any improvements. ...
3 votes
1 answer
12k views

Group a list of `dict` by all keys except one

I have a function that I use to convert a list of dictionaries into a list of tuples, similar to itertools.groupby() would do in an ideal world. The goal is to ...
3 votes
1 answer
534 views

Project Euler #58: Primes along the diagonals of a square spiral

I am working on Project Euler Problem 58: Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. ...
35 votes
10 answers
9k views

Search for a multiword string in a list of strings

Unlike Perl, you can't to my knowledge match a regular expression inside an if statement in Python and assign the result to a variable at the same moment. This ...
4 votes
2 answers
25k views

Find the median value of a list

I am taking an online training course on Python, and am at the following question: Write a function called median that takes a list as an input and returns the median value of the list. For ...
10 votes
6 answers
2k views

Median Calculator

This is just a simple median calculator that calculates the median for a given array/list. ...
5 votes
1 answer
4k views

Google FooBar Level 3 - Shortest path to 1

I recently found the Google Foobar challenges and am having a problem with level 3. The goal of the task is the find the minimum number of operations needed to transform a group of pellets down into 1,...
0 votes
1 answer
133 views

Loop through polyline vertices and update coordinate

I have a Python 2.7 script that loops through GIS polylines and updates a coordinate. The coordinate is called an "M" coordinate (aka a "Measure-value"). M coordinates are similar ...
0 votes
1 answer
39 views

What other checks do I need for removing files after X amount of days?

I am trying to remove files in my directory if they are over 30 days. However I don't know what other checks I need to do. The examples I found online wasn't very thorough besides just subtracting the ...
1 vote
1 answer
272 views

What is the pythonic way to update my nested dictionary?

So I need to update my nested dictionary where the key is "compensationsDeltaEmployee". I created a string cost_perhour to hold the value of my conditional statements. Now that I am done ...
1 vote
2 answers
428 views

Is there a faster way to write dictionary values to csv?

I have a dictionary that I am writing to a csv file, however I am only writing the values. I want to know if there is a faster way than using a for loop. I am using python 2.7 here is my code. ...
3 votes
4 answers
499 views

Find differences between two directories

Coming from another language than Python, I would like to see if my code is "pythonic" enough and follows good practices. It compares two directories, showing all files that are in one and ...
7 votes
3 answers
1k views

Guttag's Finger Exercises: Enter 10 integers, find largest odd integer and print result

I'm making my way through the Intro to Computation and Programming test by John Guttag and he gives these small exercises to practice gearing up into computational thinking. As of Ch.2, we've covered ...
4 votes
4 answers
18k views

Use Python to determine the repeating pattern in a string

I am writing an algorithm to count the number of times a substring repeats itself. The string is between 1-200 characters ranging from letters a-z. There should be no left overs at the end of the ...
3 votes
3 answers
20k views

Python 2.7 Payroll Calculator program

This is a calculator I'm working on. The ultimate goal is to calculate multiple payrolls before selecting to end the program, please review current version in that context. PSA: I'm a beginner. <...
1 vote
0 answers
97 views

Script to check a web service

I'm a Python novice. The system I'm using is called IBM Maximo Asset Management. In Maximo, we can write Python scripts that utilize Maximo's Java API/methods. So the script is technically a Jython ...
22 votes
4 answers
10k views

Regex to parse semicolon-delimited fields is too slow

I have a file with just 3500 lines like these: filecontent= "13P397;Fotostuff;t;IBM;IBM lalala 123|IBM lalala 1234;28.000 things;;IBMlalala123|IBMlalala1234" ...
8 votes
3 answers
3k views

Calculate evapotranspiration

I am trying to create a code for the Basic evapotranspiration equation by Hargreaves. I have attached a screenshot of the equation to be replicated. I want to create a class wherein I can input data ...
8 votes
3 answers
58k views

Padding a hexadecimal string with zeros

I'm given a hexadecimal number in string form with a leading "0x" that may contain 1-8 digits, but I need to pad the number with zeros so that it always has 8 ...
4 votes
3 answers
485 views

Formatting the opposite of some numbers, with decimal alignment

Background I have a list of strings containing numbers. Each string is 8 characters long. For example : ...
1 vote
1 answer
228 views

Serialize and deserialize binary search tree

Is there any good advice on how to improve performance in terms of algorithm time complexity? I am using a recursive solution and am not sure if any faster non-recursive solution exists. ...
1 vote
0 answers
911 views

Optimize Python script for memory which opens and reads multiple times the same files

I have code that works perfectly, but it uses too much memory. Essentially this code takes an input file (lets call it an index, that is 2 column tab-separated) that searches in a second input file (...
5 votes
1 answer
758 views

Send email with attachments, including in memory zipped attachments

The below class implementing a simple way to create emails with attachments, including the option to create in-memory zip files and attach them to the email. Because I am not a professional programmer ...
8 votes
2 answers
1k views

log setup using RotatingFileHandler and basicConfig

I am new to python, and developing a moderate application for which I need to setup a logging mechanism. The below program sets up log handling, which gives the user an option to specify whether log ...
4 votes
2 answers
875 views

Telegram Bot that retrieves book information from GoodReads

The project below is about a Telegram bot that implements both inline and offline modes. The idea is to get a brief info about a book, searched by title(it's author, rating, short description). I ...
7 votes
2 answers
2k views

Generate Letter Combinations of a Phone Number

The task: Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. and my solution: ...
2 votes
0 answers
34 views

Interactive, real-time bikeshare web application with Bokeh

As a side project, I'm working on a Bokeh web application to display public bikeshare data on a map. The data is updated every 2 minutes using a periodic callback. Below is the full implementation. I'...
3 votes
0 answers
50 views

Python script that getting list of all files with specific ext starting from the current folder. Using timer for reporting about the process

I am using a function that returns a list of all files (full path for each of them) with the given extension in the given folder and all subfolders. As the process is quite long and user can get ...
12 votes
1 answer
4k views

A CNN in Python WITHOUT frameworks

Here's some code that I've written for implementing a Convolutional Neural Network for recognising handwritten digits from the MNIST dataset over the last two days (after a lot of research into ...
2 votes
3 answers
601 views

Bifurcating recursive calculation with redundant calculations

def T(n): if n <= 0: return 1 else: return T(n-1) + (n-1) * T(n-2) print T(4) I need an effective way to print out the output of the function <...
2 votes
1 answer
676 views

Balanced smileys check algorithm

Problem Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced ...
8 votes
1 answer
943 views

Animation of linear transformations

Coordinates Given a 2d space, we can fix a coordinate system to talk about transformations numerically. The red vector, that ends at (1, 0) and the green vector that ends at (0, 1) are called the ...
7 votes
1 answer
914 views

Simulate object.__getattribute__ in Python

I want a Python example that illustrates how object.__getattribute__ resolves instance attribute lookups. I'm looking for feedback about the following code. Is it ...
4 votes
0 answers
160 views

Simple chat server in Python

I'm new to both Python and socket programming and figured setting up a simple chat server would be good practice. In addition to general critiques of the code, I have specific questions: What are the ...
5 votes
4 answers
712 views

Decrease Computation Time for Python Tic-Tac-Toe Minimax Algorithm

Background I am currently participating in a tic-tac-toe coding challenge. During their move, each participant is given the board state and their team, X or O. The blank board is: ...
3 votes
1 answer
50 views

Parse backslash-delimited hierarchy path into separate columns

I have rows in a database table with a hierarchypath column. The values in the column are hierarchy paths with up to four levels: ...
26 votes
6 answers
55k views

Python code to retry function

I am trying to write a python function which retries a given function until given time or function returns True with given delay. I have written the following function, but was thinking if there is ...

1
2 3 4 5
25