├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── CONTRIBUTING.md ├── Ch01 ├── 01_03 │ ├── outliers.py │ └── test_outliers.py ├── 01_04 │ └── nlp.py ├── challenge │ ├── bid_diff.py │ └── bids.csv.xz └── solution │ ├── bid_diff.py │ └── bids.csv.xz ├── Ch02 ├── 02_02 │ ├── cart.csv │ └── logs.db ├── challenge │ ├── last_error.py │ └── logs.db └── solution │ ├── last_error.py │ └── logs.db ├── Ch03 ├── 03_01 │ ├── log.txt.xz │ └── parse_log.py ├── 03_02 │ └── logs.csv.xz ├── 03_04 │ ├── ips.txt.xz │ └── logs.db ├── 03_05 │ └── logs.csv.xz ├── challenge │ ├── austin-bikes.csv.xz │ └── vacation_rides.py └── solution │ ├── austin-bikes.csv.xz │ └── vacation_rides.py ├── Ch04 ├── 04_01 │ └── austin-bikes.csv.xz ├── 04_02 │ └── logs.db ├── 04_03 │ └── taxi.csv.xz ├── challenge │ ├── austin-bikes.csv.xz │ ├── austin-kiosk.csv │ └── median_duration.py └── solution │ ├── austin-bikes.csv.xz │ ├── austin-kiosk.csv │ └── median_duration.py ├── Ch05 ├── 05_02 │ └── taxi.csv.xz ├── 05_03 │ └── taxi.csv.xz ├── 05_04 │ └── taxi.csv.xz ├── challenge │ ├── dist_changes.py │ └── taxi.csv.xz └── solution │ ├── dist_edges.py │ └── taxi.csv.xz ├── Ch06 ├── 06_02 │ ├── bikes.db │ └── session.ipy ├── 06_03 │ └── stocks.h5 ├── challenge │ ├── bikes.db │ └── ride_duration.py └── solution │ ├── bikes.db │ └── ride_duration.py ├── Ch07 ├── 07_04 │ ├── relu.pyx │ └── relu1.py ├── 07_05 │ ├── build │ │ ├── lib.linux-x86_64-3.8 │ │ │ └── relu.cpython-38-x86_64-linux-gnu.so │ │ └── temp.linux-x86_64-3.8 │ │ │ └── relu.o │ ├── relu.c │ ├── relu.cpython-38-x86_64-linux-gnu.so │ ├── relu.pyx │ ├── relu1.py │ └── setup.py ├── challenge │ └── second.py └── solution │ └── second.py ├── Ch08 ├── 08_02 │ ├── logs-1.csv.xz │ ├── logs-2.csv.xz │ ├── logs-3.csv.xz │ ├── logs-4.csv.xz │ └── logs-5.csv.xz ├── 08_03 │ └── taxi.csv.xz ├── challenge │ ├── mean_dist.py │ └── taxi.csv.xz └── solution │ ├── mean_dist.py │ └── taxi.csv.xz ├── LICENSE ├── NOTICE ├── README.md └── requirements.txt /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) deotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /Ch01/01_03/outliers.py: -------------------------------------------------------------------------------- 1 | def find_outliers(data): 2 | """Find outliers in data, return indices of outliers""" 3 | out = data[(data - data.mean()).abs() > 2 * data.std()] 4 | return out.index 5 | -------------------------------------------------------------------------------- /Ch01/01_03/test_outliers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | 4 | from outliers import find_outliers 5 | 6 | 7 | def gen_data(size, num_outliers): 8 | """Generate data in with size element containint num_outliers outliers. 9 | Returns the data and the outliers. 10 | """ 11 | regular = np.random.randint(50, 60, size-num_outliers) 12 | low = np.random.randint(1, 10, num_outliers//2) 13 | high = np.random.randint(100, 110, num_outliers-len(low)) 14 | 15 | data = np.concatenate([regular, low, high]) 16 | np.random.shuffle(data) 17 | return pd.Series(data), pd.Series(np.concatenate([low, high])) 18 | 19 | 20 | def test_bench_outliers(benchmark): 21 | size = 10_000 # Usual size of data 22 | num_outliers = 5 # Usual number of outliers 23 | data, expected = gen_data(size, num_outliers) 24 | indices = benchmark(find_outliers, data) 25 | outliers = data.loc[indices] 26 | assert set(expected) == set(outliers), 'bad result' 27 | -------------------------------------------------------------------------------- /Ch01/01_04/nlp.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | 4 | def stem(word): 5 | """Return stem of word 6 | 7 | >>> stem('working') 8 | 'work' 9 | >>> stem('works') 10 | 'work' 11 | """ 12 | return re.sub(r'(s|ing)$', '', word) 13 | 14 | 15 | def tokenize(text): 16 | """Split text to words, ignoring stop words.""" 17 | tokens = [] 18 | for tok in re.findall('[a-zA-Z]+', text): 19 | tok = tok.lower() 20 | tok = stem(tok) 21 | if tok not in stop_words: 22 | tokens.append(tok) 23 | return tokens 24 | 25 | 26 | stop_words = { 27 | 'a', 'able', 'about', 'across', 'after', 'all', 'almost', 'also', 'am', 28 | 'among', 'an', 'and', 'any', 'are', 'as', 'at', 'be', 'because', 'been', 29 | 'but', 'by', 'can', 'cannot', 'could', 'dear', 'did', 'do', 'does', 30 | 'either', 'else', 'ever', 'every', 'for', 'from', 'get', 'got', 'had', 31 | 'has', 'have', 'he', 'her', 'hers', 'him', 'his', 'how', 'however', 'i', 32 | 'if', 'in', 'into', 'is', 'it', 'its', 'just', 'least', 'let', 'like', 33 | 'likely', 'may', 'me', 'might', 'most', 'must', 'my', 'neither', 'no', 34 | 'nor', 'not', 'of', 'off', 'often', 'on', 'only', 'or', 'other', 'our', 35 | 'own', 'rather', 'said', 'say', 'says', 'she', 'should', 'since', 'so', 36 | 'some', 'than', 'that', 'the', 'their', 'them', 'then', 'there', 'these', 37 | 'they', 'this', 'tis', 'to', 'too', 'twas', 'us', 'wants', 'was', 'we', 38 | 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'whom', 'why', 39 | 'will', 'with', 'would', 'yet', 'you', 'your', 40 | } 41 | -------------------------------------------------------------------------------- /Ch01/challenge/bid_diff.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def second(values): 5 | """Return second highest value 6 | 7 | >>> second([1, 7, 9, 3, 5]) 8 | 7 9 | """ 10 | top, second = -1, -1 11 | for value in values: 12 | if value > top: 13 | top, second = value, top 14 | elif value > second: 15 | second = value 16 | return second 17 | 18 | 19 | def median_diff(csv_file): 20 | df = pd.read_csv(csv_file) 21 | top1 = df.groupby('id')['price'].max() 22 | top2 = df.groupby('id')['price'].apply(second) 23 | diffs = top1 - top2 24 | return diffs.median() 25 | -------------------------------------------------------------------------------- /Ch01/challenge/bids.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch01/challenge/bids.csv.xz -------------------------------------------------------------------------------- /Ch01/solution/bid_diff.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def second(values): 5 | """Return second highest value 6 | 7 | >>> second([1, 7, 9, 3, 5]) 8 | 7 9 | """ 10 | top, second = -1, -1 11 | for value in values: 12 | if value > top: 13 | top, second = value, top 14 | elif value > second: 15 | second = value 16 | return second 17 | 18 | 19 | def median_diff(csv_file): 20 | df = pd.read_csv(csv_file) 21 | top1 = df.groupby('id')['price'].max() 22 | top2 = df.groupby('id')['price'].apply(second) 23 | diffs = top1 - top2 24 | return diffs.median() 25 | -------------------------------------------------------------------------------- /Ch01/solution/bids.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch01/solution/bids.csv.xz -------------------------------------------------------------------------------- /Ch02/02_02/cart.csv: -------------------------------------------------------------------------------- 1 | Customer,Item,Amount,Item Price 2 | Rick,Wine,20,103.2 3 | Morty,Almond Milk,1,10.04 4 | Summer,Ice Cream,1,8.32 5 | Beth,Comb,1,7.3 6 | Jerry,Tequila,2,20.34 7 | -------------------------------------------------------------------------------- /Ch02/02_02/logs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch02/02_02/logs.db -------------------------------------------------------------------------------- /Ch02/challenge/last_error.py: -------------------------------------------------------------------------------- 1 | """Find last time we had error in logs""" 2 | 3 | import sqlite3 4 | from contextlib import closing 5 | 6 | import pandas as pd 7 | 8 | 9 | def last_error_time(df): 10 | """Find last time there's an error in df""" 11 | last_time = None 12 | for _, row in df.iterrows(): 13 | if row['status_code'] < 400: 14 | continue 15 | if not last_time or row['time'] > last_time: 16 | last_time = row['time'] 17 | return last_time 18 | 19 | 20 | def load_df(db_file): 21 | """Load DataFrame from database""" 22 | conn = sqlite3.connect(db_file, detect_types=sqlite3.PARSE_DECLTYPES) 23 | with closing(conn): 24 | return pd.read_sql('SELECT * FROM logs', conn) 25 | -------------------------------------------------------------------------------- /Ch02/challenge/logs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch02/challenge/logs.db -------------------------------------------------------------------------------- /Ch02/solution/last_error.py: -------------------------------------------------------------------------------- 1 | """Find last time we had error in logs""" 2 | 3 | import sqlite3 4 | from contextlib import closing 5 | 6 | import pandas as pd 7 | 8 | 9 | def last_error_time(df): 10 | """Find last time there's an error in df""" 11 | return df[df['status_code'] >= 400]['time'].max() 12 | 13 | 14 | def load_df(db_file): 15 | """Load DataFrame from database""" 16 | conn = sqlite3.connect(db_file, detect_types=sqlite3.PARSE_DECLTYPES) 17 | with closing(conn): 18 | return pd.read_sql('SELECT * FROM logs', conn) 19 | -------------------------------------------------------------------------------- /Ch02/solution/logs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch02/solution/logs.db -------------------------------------------------------------------------------- /Ch03/03_01/log.txt.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/03_01/log.txt.xz -------------------------------------------------------------------------------- /Ch03/03_01/parse_log.py: -------------------------------------------------------------------------------- 1 | """Parse log lines""" 2 | 3 | from datetime import datetime 4 | 5 | 6 | def parse_time(ts): 7 | # [02/Jul/1995:16:30:08 -0400] 8 | time = datetime.strptime(ts, '[%d/%b/%Y:%H:%M:%S %z]') 9 | return time.replace(tzinfo=None) # Remove time zone 10 | 11 | 12 | def parse_line(line): 13 | fields = line.split() 14 | size = 0 if fields[-1] == '-' else int(fields[-1]) 15 | return { 16 | 'origin': fields[0], 17 | 'time': parse_time(fields[3] + ' ' + fields[4]), 18 | 'method': fields[5][1:], # Remove leading " 19 | 'path': fields[6], 20 | 'status_code': int(fields[-2]), 21 | 'size': size, 22 | } 23 | -------------------------------------------------------------------------------- /Ch03/03_02/logs.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/03_02/logs.csv.xz -------------------------------------------------------------------------------- /Ch03/03_04/ips.txt.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/03_04/ips.txt.xz -------------------------------------------------------------------------------- /Ch03/03_04/logs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/03_04/logs.db -------------------------------------------------------------------------------- /Ch03/03_05/logs.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/03_05/logs.csv.xz -------------------------------------------------------------------------------- /Ch03/challenge/austin-bikes.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/challenge/austin-bikes.csv.xz -------------------------------------------------------------------------------- /Ch03/challenge/vacation_rides.py: -------------------------------------------------------------------------------- 1 | """Find how many rides in 2016 were in the afternoon of weekend or holiday. 2 | 3 | - Afternoon: Between noon to 6pm 4 | - Weekend: Saturday or Sunday 5 | - Holiday: See holidays_2016 below 6 | """ 7 | 8 | import pandas as pd 9 | 10 | # 2016 public holidays 11 | holidays_2016 = [ 12 | '2016-01-01', # new year 13 | '2016-01-18', # MLK 14 | '2016-05-30', # memorial 15 | '2016-07-04', # independence 16 | '2016-09-05', # labor 17 | '2016-11-11', # veterans 18 | '2016-11-24', # thanksgiving 19 | '2016-12-26', # christmas 20 | ] 21 | 22 | 23 | def load_df(file_name): 24 | """Load data from CSV to DataFrame""" 25 | return pd.read_csv(file_name) 26 | 27 | 28 | def is_2016(s): 29 | ts = pd.to_datetime(s) 30 | 31 | return ts.year == 2016 32 | 33 | 34 | def is_weekend(s): 35 | """Check that s in a weekend day""" 36 | ts = pd.to_datetime(s) 37 | 38 | return ts.day_name() == 'Saturday' or ts.day_name() == 'Sunday' 39 | 40 | 41 | def is_holiday(s): 42 | """Check that s (e.g. '10/26/2014') is a holiday""" 43 | ts = pd.to_datetime(s) 44 | 45 | day = ts.strftime('%Y-%m-%d') # holidays_2016 format 46 | return day in holidays_2016 47 | 48 | 49 | def is_afternoon(s): 50 | """Check that s (e.g. '13:12:00' is in the afternoon""" 51 | ts = pd.to_datetime(s) 52 | 53 | return ts.hour >= 12 and ts.hour < 18 54 | 55 | 56 | def vacation_rides(df): 57 | """Return only rows that are in holiday afternoon""" 58 | result = pd.DataFrame() 59 | for _, row in df.iterrows(): 60 | date, time = row['Checkout Date'], row['Checkout Time'] 61 | if not is_2016(date): 62 | continue 63 | 64 | if (is_holiday(date) or is_weekend(date)) and is_afternoon(time): 65 | result = result.append(row, ignore_index=True) 66 | 67 | return result 68 | -------------------------------------------------------------------------------- /Ch03/solution/austin-bikes.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch03/solution/austin-bikes.csv.xz -------------------------------------------------------------------------------- /Ch03/solution/vacation_rides.py: -------------------------------------------------------------------------------- 1 | """Find how many rides in 2016 were in the afternoon of weekend or holiday. 2 | 3 | - Afternoon: Between noon to 6pm 4 | - Weekend: Friday or Saturday 5 | - Holiday: See holidays_2016 below 6 | """ 7 | 8 | from calendar import SATURDAY, SUNDAY 9 | 10 | import pandas as pd 11 | 12 | # 2016 public holidays 13 | holidays_2016 = pd.to_datetime([ 14 | '2016-01-01', # new year 15 | '2016-01-18', # MLK 16 | '2016-05-30', # memorial 17 | '2016-07-04', # independence 18 | '2016-09-05', # labor 19 | '2016-11-11', # veterans 20 | '2016-11-24', # thanksgiving 21 | '2016-12-26', # christmas 22 | ]) 23 | 24 | 25 | def load_df(file_name): 26 | """Load data from CSV to DataFrame""" 27 | return pd.read_csv( 28 | file_name, 29 | parse_dates={'time': ['Checkout Date', 'Checkout Time']}, 30 | ) 31 | 32 | 33 | def vacation_rides(df): 34 | """Return only rows that are in holiday afternoon""" 35 | mask_2016 = df['time'].dt.year == 2016 36 | 37 | holiday_mask = ( 38 | (df['time'].dt.floor('d').isin(holidays_2016)) | 39 | (df['time'].dt.weekday.isin([SATURDAY, SUNDAY])) 40 | ) 41 | 42 | afternoon_mask = (df['time'].dt.hour >= 12) & (df['time'].dt.hour < 18) 43 | 44 | return df[mask_2016 & holiday_mask & afternoon_mask] 45 | -------------------------------------------------------------------------------- /Ch04/04_01/austin-bikes.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch04/04_01/austin-bikes.csv.xz -------------------------------------------------------------------------------- /Ch04/04_02/logs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch04/04_02/logs.db -------------------------------------------------------------------------------- /Ch04/04_03/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch04/04_03/taxi.csv.xz -------------------------------------------------------------------------------- /Ch04/challenge/austin-bikes.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch04/challenge/austin-bikes.csv.xz -------------------------------------------------------------------------------- /Ch04/challenge/austin-kiosk.csv: -------------------------------------------------------------------------------- 1 | Kiosk ID,Kiosk Name,Kiosk Status,Latitude,Longitude,Location 2 | 3793,Rio Grande & 28th,active,30.29333,-97.74412,"(30.29333, -97.74412)" 3 | 3291,11th & San Jacinto,active,30.27193,-97.73854,"(30.27193, -97.73854)" 4 | 4058,Hollow Creek & Barton Hills,active,30.26139,-97.77234,"(30.26139, -97.77234)" 5 | 3797,21st & University,active,30.28354,-97.73953,"(30.28354, -97.73953)" 6 | 2546,ACC - West & 12th Street,closed,30.27624,-97.74831,"(30.27624, -97.74831)" 7 | 3838,Nueces & 26th,active,30.29068,-97.74292,"(30.29068, -97.74292)" 8 | 2544,East 6th & Pedernales St.,active,30.25895,-97.71475,"(30.25895, -97.71475)" 9 | 3390,Brazos & 6th,active,30.26754,-97.74154,"(30.26754, -97.74154)" 10 | 1008,Nueces @ 3rd,closed,30.26694,-97.74939,"(30.26694, -97.74939)" 11 | 2823,Capital Metro HQ - East 5th at Broadway,active,30.2563,-97.71007,"(30.2563, -97.71007)" 12 | 2550,Republic Square @ Guadalupe & 4th St.,closed,30.26774,-97.74692,"(30.26774, -97.74692)" 13 | 2538,Bullock Museum @ Congress & MLK,closed,30.28039,-97.73809,"(30.28039, -97.73809)" 14 | 2711,Barton Springs @ Kinney Ave,active,30.262,-97.76118,"(30.262, -97.76118)" 15 | 3685,Henderson & 9th,active,30.27217,-97.75246,"(30.27217, -97.75246)" 16 | 4052,Rosewood & Angelina,active,30.26888,-97.72431,"(30.26888, -97.72431)" 17 | 3621,Nueces & 3rd,active,30.26697,-97.74929,"(30.26697, -97.74929)" 18 | 3686,Sterzing at Barton Springs,active,30.26406,-97.76385,"(30.26406, -97.76385)" 19 | 3684,Congress & Cesar Chavez,active,30.26332,-97.74508,"(30.26332, -97.74508)" 20 | 4055,11th & Salina,active,30.26638,-97.7214,"(30.26638, -97.7214)" 21 | 2566,Pfluger Bridge @ W 2nd Street,active,30.26717,-97.75484,"(30.26717, -97.75484)" 22 | 4059,Nash Hernandez @ RBJ South,active,30.25189,-97.73323,"(30.25189, -97.73323)" 23 | 3635,13th & San Antonio,active,30.27616,-97.74488,"(30.27616, -97.74488)" 24 | 3513,South Congress & Barton Springs at the Austin American-Statesman,active,30.25839,-97.74592,"(30.25839, -97.74592)" 25 | 1002,6th & Navasota St.,closed,30.26383,-97.72864,"(30.26383, -97.72864)" 26 | 2541,State Capitol @ 14th & Colorado,closed,30.27654,-97.74155,"(30.27654, -97.74155)" 27 | 4050,5th & Campbell,active,30.27489,-97.76483,"(30.27489, -97.76483)" 28 | 2499,City Hall / Lavaca & 2nd,active,30.26476,-97.74678,"(30.26476, -97.74678)" 29 | 2568,East 11th St. at Victory Grill,active,30.26896,-97.72843,"(30.26896, -97.72843)" 30 | 2575,Riverside @ S. Lamar,active,30.26446,-97.75665,"(30.26446, -97.75665)" 31 | 2502,Barton Springs & Riverside,active,30.2587,-97.74872,"(30.2587, -97.74872)" 32 | 2563,Davis at Rainey Street,active,30.26019,-97.73845,"(30.26019, -97.73845)" 33 | 4061,Lakeshore @ Austin Hostel,active,30.24472,-97.72336,"(30.24472, -97.72336)" 34 | 2707,Rainey St @ Cummings,active,30.25579,-97.73982,"(30.25579, -97.73982)" 35 | 2545,ACC - Rio Grande & 12th,closed,30.27595,-97.74739,"(30.27595, -97.74739)" 36 | 2549,Long Center @ South 1st & Riverside,active,30.25941,-97.74971,"(30.25941, -97.74971)" 37 | 2561,State Capitol Visitors Garage @ San Jacinto & 12th,active,30.27336,-97.73805,"(30.27336, -97.73805)" 38 | 2494,2nd & Congress,active,30.26408,-97.74355,"(30.26408, -97.74355)" 39 | 3464,Pease Park,closed,30.28118,-97.75219,"(30.28118, -97.75219)" 40 | 2570,South Congress & Academy,active,30.25226,-97.74854,"(30.25226, -97.74854)" 41 | 2540,17th & Guadalupe,active,30.27974,-97.74254,"(30.27974, -97.74254)" 42 | 2498,Convention Center / 4th St. @ MetroRail,active,30.26483,-97.739,"(30.26483, -97.739)" 43 | 3794,Dean Keeton & Speedway,active,30.28953,-97.73695,"(30.28953, -97.73695)" 44 | 2547,Guadalupe & 21st,active,30.28395,-97.74198,"(30.28395, -97.74198)" 45 | 2712,Toomey Rd @ South Lamar,closed,30.26304,-97.75824,"(30.26304, -97.75824)" 46 | 2501,5th & Bowie,active,30.2696,-97.75332,"(30.2696, -97.75332)" 47 | 2572,Barton Springs Pool,active,30.26452,-97.7712,"(30.26452, -97.7712)" 48 | 2576,Rainey @ River St,closed,30.25802,-97.7391,"(30.25802, -97.7391)" 49 | 2567,Palmer Auditorium,active,30.25971,-97.75346,"(30.25971, -97.75346)" 50 | 3381,East 7th & Pleasant Valley,closed,30.26025,-97.71002,"(30.26025, -97.71002)" 51 | 1006,Zilker Park West,closed,30.26587,-97.76826,"(30.26587, -97.76826)" 52 | 3799,23rd & San Jacinto @ DKR Stadium,active,30.2856,-97.7335,"(30.2856, -97.7335)" 53 | 3660,Medina & East 6th,active,30.26455,-97.73165,"(30.26455, -97.73165)" 54 | 3791,Lake Austin & Enfield,active,30.29439,-97.78375,"(30.29439, -97.78375)" 55 | 4057,6th & Chalmers,active,30.26269,-97.72438,"(30.26269, -97.72438)" 56 | 2537,West & 6th St.,active,30.27041,-97.75046,"(30.27041, -97.75046)" 57 | 2503,South Congress & James,active,30.25103,-97.74926,"(30.25103, -97.74926)" 58 | 3792,22nd & Pearl,active,30.2853,-97.7467,"(30.2853, -97.7467)" 59 | 3293,East 2nd & Pedernales,active,30.25542,-97.71665,"(30.25542, -97.71665)" 60 | 4062,Lakeshore & Pleasant Valley,active,30.24258,-97.71726,"(30.24258, -97.71726)" 61 | 2571,Red River & 8th Street,active,30.26854,-97.73646,"(30.26854, -97.73646)" 62 | 3798,21st & Speedway @PCL,active,30.283,-97.7375,"(30.283, -97.7375)" 63 | 2548,UT West Mall @ Guadalupe,active,30.28576,-97.74181,"(30.28576, -97.74181)" 64 | 3795,Dean Keeton & Whitis,active,30.2898,-97.74041,"(30.2898, -97.74041)" 65 | 2500,Republic Square,closed,30.26751,-97.74802,"(30.26751, -97.74802)" 66 | 2542,Plaza Saltillo,active,30.26217,-97.72743,"(30.26217, -97.72743)" 67 | 3619,6th & Congress,active,30.26822,-97.74285,"(30.26822, -97.74285)" 68 | 4048,South Congress @ Bouldin Creek,active,30.25495,-97.74755,"(30.25495, -97.74755)" 69 | 3790,Lake Austin Blvd @ Deep Eddy,active,30.27807,-97.77272,"(30.27807, -97.77272)" 70 | 2536,Waller & 6th St.,closed,30.26461,-97.73049,"(30.26461, -97.73049)" 71 | 2495,4th & Congress,active,30.26634,-97.74378,"(30.26634, -97.74378)" 72 | 1007,Lavaca & 6th,closed,30.26889,-97.74525,"(30.26889, -97.74525)" 73 | 3841,23rd & Rio Grande,active,30.28728,-97.74495,"(30.28728, -97.74495)" 74 | 2562,San Jacinto & 8th Street,active,30.26912,-97.73986,"(30.26912, -97.73986)" 75 | 2822,East 6th at Robert Martinez,active,30.26032,-97.71899,"(30.26032, -97.71899)" 76 | 4047,8th & Lavaca,active,30.27059,-97.74441,"(30.27059, -97.74441)" 77 | 3292,East 4th & Chicon,active,30.25987,-97.72373,"(30.25987, -97.72373)" 78 | 2497,Capitol Station / Congress & 11th,active,30.2726,-97.74127,"(30.2726, -97.74127)" 79 | 3377,MoPac Pedestrian Bridge @ Veterans Drive,active,30.27466,-97.77028,"(30.27466, -97.77028)" 80 | 3687,Boardwalk West,active,30.25457,-97.74258,"(30.25457, -97.74258)" 81 | 1003,8th & Guadalupe,closed,30.27106,-97.74563,"(30.27106, -97.74563)" 82 | 2504,South Congress & Elizabeth,active,30.24891,-97.75019,"(30.24891, -97.75019)" 83 | 1001,OFFICE/Main/Shop/Repair,active,30.27186,-97.73997,"(30.27186, -97.73997)" 84 | 2574,Zilker Park,active,30.2659,-97.76822,"(30.2659, -97.76822)" 85 | 2569,East 11th St. & San Marcos,active,30.26968,-97.73074,"(30.26968, -97.73074)" 86 | 2539,Convention Center / 3rd & Trinity,active,30.26426,-97.74023,"(30.26426, -97.74023)" 87 | 3294,Lavaca & 6th,active,30.26911,-97.7462,"(30.26911, -97.7462)" 88 | 2496,8th & Congress,active,30.2698,-97.74186,"(30.2698, -97.74186)" 89 | 2564,5th & San Marcos,closed,30.26416,-97.73289,"(30.26416, -97.73289)" 90 | 2565,Trinity & 6th Street,active,30.26735,-97.73933,"(30.26735, -97.73933)" 91 | 4054,Rosewood & Chicon,active,30.26969,-97.71873,"(30.26969, -97.71873)" 92 | 1004,Red River & LBJ Library,closed,30.2848,-97.72756,"(30.2848, -97.72756)" 93 | 2552,3rd & West,active,30.2678,-97.75189,"(30.2678, -97.75189)" 94 | 3455,Republic Square @ 5th & Guadalupe,active,30.26753,-97.74805,"(30.26753, -97.74805)" 95 | 1005,State Parking Garage @ Brazos & 18th,closed,30.27907,-97.73715,"(30.27907, -97.73715)" 96 | 4060,Red River/Cesar Chavez @ The Fairmont,active,30.26212,-97.73815,"(30.26212, -97.73815)" 97 | 4051,10th & Red River,active,30.27024,-97.73578,"(30.27024, -97.73578)" 98 | -------------------------------------------------------------------------------- /Ch04/challenge/median_duration.py: -------------------------------------------------------------------------------- 1 | """What is the median trip duration in 2017, only in active kiosks? 2 | 3 | - Trip data in austin-bikes.csv.xz 4 | - Kiosk status data in austin-kiosk.csv 5 | """ 6 | -------------------------------------------------------------------------------- /Ch04/solution/austin-bikes.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch04/solution/austin-bikes.csv.xz -------------------------------------------------------------------------------- /Ch04/solution/austin-kiosk.csv: -------------------------------------------------------------------------------- 1 | Kiosk ID,Kiosk Name,Kiosk Status,Latitude,Longitude,Location 2 | 3793,Rio Grande & 28th,active,30.29333,-97.74412,"(30.29333, -97.74412)" 3 | 3291,11th & San Jacinto,active,30.27193,-97.73854,"(30.27193, -97.73854)" 4 | 4058,Hollow Creek & Barton Hills,active,30.26139,-97.77234,"(30.26139, -97.77234)" 5 | 3797,21st & University,active,30.28354,-97.73953,"(30.28354, -97.73953)" 6 | 2546,ACC - West & 12th Street,closed,30.27624,-97.74831,"(30.27624, -97.74831)" 7 | 3838,Nueces & 26th,active,30.29068,-97.74292,"(30.29068, -97.74292)" 8 | 2544,East 6th & Pedernales St.,active,30.25895,-97.71475,"(30.25895, -97.71475)" 9 | 3390,Brazos & 6th,active,30.26754,-97.74154,"(30.26754, -97.74154)" 10 | 1008,Nueces @ 3rd,closed,30.26694,-97.74939,"(30.26694, -97.74939)" 11 | 2823,Capital Metro HQ - East 5th at Broadway,active,30.2563,-97.71007,"(30.2563, -97.71007)" 12 | 2550,Republic Square @ Guadalupe & 4th St.,closed,30.26774,-97.74692,"(30.26774, -97.74692)" 13 | 2538,Bullock Museum @ Congress & MLK,closed,30.28039,-97.73809,"(30.28039, -97.73809)" 14 | 2711,Barton Springs @ Kinney Ave,active,30.262,-97.76118,"(30.262, -97.76118)" 15 | 3685,Henderson & 9th,active,30.27217,-97.75246,"(30.27217, -97.75246)" 16 | 4052,Rosewood & Angelina,active,30.26888,-97.72431,"(30.26888, -97.72431)" 17 | 3621,Nueces & 3rd,active,30.26697,-97.74929,"(30.26697, -97.74929)" 18 | 3686,Sterzing at Barton Springs,active,30.26406,-97.76385,"(30.26406, -97.76385)" 19 | 3684,Congress & Cesar Chavez,active,30.26332,-97.74508,"(30.26332, -97.74508)" 20 | 4055,11th & Salina,active,30.26638,-97.7214,"(30.26638, -97.7214)" 21 | 2566,Pfluger Bridge @ W 2nd Street,active,30.26717,-97.75484,"(30.26717, -97.75484)" 22 | 4059,Nash Hernandez @ RBJ South,active,30.25189,-97.73323,"(30.25189, -97.73323)" 23 | 3635,13th & San Antonio,active,30.27616,-97.74488,"(30.27616, -97.74488)" 24 | 3513,South Congress & Barton Springs at the Austin American-Statesman,active,30.25839,-97.74592,"(30.25839, -97.74592)" 25 | 1002,6th & Navasota St.,closed,30.26383,-97.72864,"(30.26383, -97.72864)" 26 | 2541,State Capitol @ 14th & Colorado,closed,30.27654,-97.74155,"(30.27654, -97.74155)" 27 | 4050,5th & Campbell,active,30.27489,-97.76483,"(30.27489, -97.76483)" 28 | 2499,City Hall / Lavaca & 2nd,active,30.26476,-97.74678,"(30.26476, -97.74678)" 29 | 2568,East 11th St. at Victory Grill,active,30.26896,-97.72843,"(30.26896, -97.72843)" 30 | 2575,Riverside @ S. Lamar,active,30.26446,-97.75665,"(30.26446, -97.75665)" 31 | 2502,Barton Springs & Riverside,active,30.2587,-97.74872,"(30.2587, -97.74872)" 32 | 2563,Davis at Rainey Street,active,30.26019,-97.73845,"(30.26019, -97.73845)" 33 | 4061,Lakeshore @ Austin Hostel,active,30.24472,-97.72336,"(30.24472, -97.72336)" 34 | 2707,Rainey St @ Cummings,active,30.25579,-97.73982,"(30.25579, -97.73982)" 35 | 2545,ACC - Rio Grande & 12th,closed,30.27595,-97.74739,"(30.27595, -97.74739)" 36 | 2549,Long Center @ South 1st & Riverside,active,30.25941,-97.74971,"(30.25941, -97.74971)" 37 | 2561,State Capitol Visitors Garage @ San Jacinto & 12th,active,30.27336,-97.73805,"(30.27336, -97.73805)" 38 | 2494,2nd & Congress,active,30.26408,-97.74355,"(30.26408, -97.74355)" 39 | 3464,Pease Park,closed,30.28118,-97.75219,"(30.28118, -97.75219)" 40 | 2570,South Congress & Academy,active,30.25226,-97.74854,"(30.25226, -97.74854)" 41 | 2540,17th & Guadalupe,active,30.27974,-97.74254,"(30.27974, -97.74254)" 42 | 2498,Convention Center / 4th St. @ MetroRail,active,30.26483,-97.739,"(30.26483, -97.739)" 43 | 3794,Dean Keeton & Speedway,active,30.28953,-97.73695,"(30.28953, -97.73695)" 44 | 2547,Guadalupe & 21st,active,30.28395,-97.74198,"(30.28395, -97.74198)" 45 | 2712,Toomey Rd @ South Lamar,closed,30.26304,-97.75824,"(30.26304, -97.75824)" 46 | 2501,5th & Bowie,active,30.2696,-97.75332,"(30.2696, -97.75332)" 47 | 2572,Barton Springs Pool,active,30.26452,-97.7712,"(30.26452, -97.7712)" 48 | 2576,Rainey @ River St,closed,30.25802,-97.7391,"(30.25802, -97.7391)" 49 | 2567,Palmer Auditorium,active,30.25971,-97.75346,"(30.25971, -97.75346)" 50 | 3381,East 7th & Pleasant Valley,closed,30.26025,-97.71002,"(30.26025, -97.71002)" 51 | 1006,Zilker Park West,closed,30.26587,-97.76826,"(30.26587, -97.76826)" 52 | 3799,23rd & San Jacinto @ DKR Stadium,active,30.2856,-97.7335,"(30.2856, -97.7335)" 53 | 3660,Medina & East 6th,active,30.26455,-97.73165,"(30.26455, -97.73165)" 54 | 3791,Lake Austin & Enfield,active,30.29439,-97.78375,"(30.29439, -97.78375)" 55 | 4057,6th & Chalmers,active,30.26269,-97.72438,"(30.26269, -97.72438)" 56 | 2537,West & 6th St.,active,30.27041,-97.75046,"(30.27041, -97.75046)" 57 | 2503,South Congress & James,active,30.25103,-97.74926,"(30.25103, -97.74926)" 58 | 3792,22nd & Pearl,active,30.2853,-97.7467,"(30.2853, -97.7467)" 59 | 3293,East 2nd & Pedernales,active,30.25542,-97.71665,"(30.25542, -97.71665)" 60 | 4062,Lakeshore & Pleasant Valley,active,30.24258,-97.71726,"(30.24258, -97.71726)" 61 | 2571,Red River & 8th Street,active,30.26854,-97.73646,"(30.26854, -97.73646)" 62 | 3798,21st & Speedway @PCL,active,30.283,-97.7375,"(30.283, -97.7375)" 63 | 2548,UT West Mall @ Guadalupe,active,30.28576,-97.74181,"(30.28576, -97.74181)" 64 | 3795,Dean Keeton & Whitis,active,30.2898,-97.74041,"(30.2898, -97.74041)" 65 | 2500,Republic Square,closed,30.26751,-97.74802,"(30.26751, -97.74802)" 66 | 2542,Plaza Saltillo,active,30.26217,-97.72743,"(30.26217, -97.72743)" 67 | 3619,6th & Congress,active,30.26822,-97.74285,"(30.26822, -97.74285)" 68 | 4048,South Congress @ Bouldin Creek,active,30.25495,-97.74755,"(30.25495, -97.74755)" 69 | 3790,Lake Austin Blvd @ Deep Eddy,active,30.27807,-97.77272,"(30.27807, -97.77272)" 70 | 2536,Waller & 6th St.,closed,30.26461,-97.73049,"(30.26461, -97.73049)" 71 | 2495,4th & Congress,active,30.26634,-97.74378,"(30.26634, -97.74378)" 72 | 1007,Lavaca & 6th,closed,30.26889,-97.74525,"(30.26889, -97.74525)" 73 | 3841,23rd & Rio Grande,active,30.28728,-97.74495,"(30.28728, -97.74495)" 74 | 2562,San Jacinto & 8th Street,active,30.26912,-97.73986,"(30.26912, -97.73986)" 75 | 2822,East 6th at Robert Martinez,active,30.26032,-97.71899,"(30.26032, -97.71899)" 76 | 4047,8th & Lavaca,active,30.27059,-97.74441,"(30.27059, -97.74441)" 77 | 3292,East 4th & Chicon,active,30.25987,-97.72373,"(30.25987, -97.72373)" 78 | 2497,Capitol Station / Congress & 11th,active,30.2726,-97.74127,"(30.2726, -97.74127)" 79 | 3377,MoPac Pedestrian Bridge @ Veterans Drive,active,30.27466,-97.77028,"(30.27466, -97.77028)" 80 | 3687,Boardwalk West,active,30.25457,-97.74258,"(30.25457, -97.74258)" 81 | 1003,8th & Guadalupe,closed,30.27106,-97.74563,"(30.27106, -97.74563)" 82 | 2504,South Congress & Elizabeth,active,30.24891,-97.75019,"(30.24891, -97.75019)" 83 | 1001,OFFICE/Main/Shop/Repair,active,30.27186,-97.73997,"(30.27186, -97.73997)" 84 | 2574,Zilker Park,active,30.2659,-97.76822,"(30.2659, -97.76822)" 85 | 2569,East 11th St. & San Marcos,active,30.26968,-97.73074,"(30.26968, -97.73074)" 86 | 2539,Convention Center / 3rd & Trinity,active,30.26426,-97.74023,"(30.26426, -97.74023)" 87 | 3294,Lavaca & 6th,active,30.26911,-97.7462,"(30.26911, -97.7462)" 88 | 2496,8th & Congress,active,30.2698,-97.74186,"(30.2698, -97.74186)" 89 | 2564,5th & San Marcos,closed,30.26416,-97.73289,"(30.26416, -97.73289)" 90 | 2565,Trinity & 6th Street,active,30.26735,-97.73933,"(30.26735, -97.73933)" 91 | 4054,Rosewood & Chicon,active,30.26969,-97.71873,"(30.26969, -97.71873)" 92 | 1004,Red River & LBJ Library,closed,30.2848,-97.72756,"(30.2848, -97.72756)" 93 | 2552,3rd & West,active,30.2678,-97.75189,"(30.2678, -97.75189)" 94 | 3455,Republic Square @ 5th & Guadalupe,active,30.26753,-97.74805,"(30.26753, -97.74805)" 95 | 1005,State Parking Garage @ Brazos & 18th,closed,30.27907,-97.73715,"(30.27907, -97.73715)" 96 | 4060,Red River/Cesar Chavez @ The Fairmont,active,30.26212,-97.73815,"(30.26212, -97.73815)" 97 | 4051,10th & Red River,active,30.27024,-97.73578,"(30.27024, -97.73578)" 98 | -------------------------------------------------------------------------------- /Ch04/solution/median_duration.py: -------------------------------------------------------------------------------- 1 | """What is the median trip duration in 2017, only in active kiosks? 2 | 3 | - Trip data in austin-bikes.csv.xz 4 | - Kiosk status data in austin-kiosk.csv 5 | """ 6 | 7 | import pandas as pd 8 | 9 | bike_df = pd.read_csv('austin-bikes.csv.xz', low_memory=False) 10 | 11 | # Set index to 'Kiosk ID' for faster merge 12 | kiosk_df = pd.read_csv('austin-kiosk.csv', index_col='Kiosk ID') 13 | df = pd.merge( 14 | bike_df, kiosk_df, left_on='Checkout Kiosk ID', right_index=True) 15 | 16 | # Use query for selecting data 17 | active_2017 = df.query( 18 | '`Kiosk Status` == "active" & Year == 2017 & `Trip Duration Minutes` > 0') 19 | 20 | # Use built-in median 21 | print(active_2017['Trip Duration Minutes'].median()) 22 | -------------------------------------------------------------------------------- /Ch05/05_02/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch05/05_02/taxi.csv.xz -------------------------------------------------------------------------------- /Ch05/05_03/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch05/05_03/taxi.csv.xz -------------------------------------------------------------------------------- /Ch05/05_04/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch05/05_04/taxi.csv.xz -------------------------------------------------------------------------------- /Ch05/challenge/dist_changes.py: -------------------------------------------------------------------------------- 1 | """Calculate the minimal and maximal distance driven from the data at 2 | taxi.csv.xz 3 | Consume as little memory as possible and don't load more than 50,000 rows at a 4 | time. 5 | """ 6 | 7 | import pandas as pd 8 | -------------------------------------------------------------------------------- /Ch05/challenge/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch05/challenge/taxi.csv.xz -------------------------------------------------------------------------------- /Ch05/solution/dist_edges.py: -------------------------------------------------------------------------------- 1 | """Calculate the minimal and maximal distance driven from the data at 2 | taxi.csv.xz 3 | Consume as little memory as possible and don't load more than 50,000 rows at a 4 | time. 5 | """ 6 | 7 | import pandas as pd 8 | 9 | min_dist, max_dist = float('inf'), float('-inf') 10 | dfs = pd.read_csv('taxi.csv.xz', usecols=['trip_distance'], chunksize=50_000) 11 | for df in dfs: 12 | desc = df['trip_distance'].describe() 13 | min_dist = min(min_dist, desc['min']) 14 | max_dist = max(max_dist, desc['max']) 15 | 16 | print('min', min_dist, 'max', max_dist) 17 | -------------------------------------------------------------------------------- /Ch05/solution/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch05/solution/taxi.csv.xz -------------------------------------------------------------------------------- /Ch06/06_02/bikes.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch06/06_02/bikes.db -------------------------------------------------------------------------------- /Ch06/06_02/session.ipy: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | conn = sqlite3.connect('bikes.db') 3 | sql = ''' 4 | SELECT name, sql 5 | FROM sqlite_master 6 | WHERE type = 'table' 7 | ''' 8 | for name, sql in conn.execute(sql): 9 | print(name) 10 | print(sql) 11 | 12 | # bike_rides 13 | # CREATE TABLE bike_rides( 14 | # year INT, 15 | # month INT, 16 | # day INT, 17 | # trip_id INT, 18 | # bike_id INT, 19 | # duration INT 20 | # ) 21 | 22 | conn.execute('SELECT MIN(year), MAX(year) FROM bike_rides').fetchall() 23 | # [(2016, 2017)] 24 | 25 | import pandas as pd 26 | 27 | query = ''' 28 | SELECT year, month, duration 29 | FROM bike_rides 30 | WHERE year == 2016 OR year == 2017 31 | ''' 32 | df = pd.read_sql(query, conn) 33 | df.groupby(['year', 'month'])['duration'].median() 34 | # year month 35 | # 2016 1 14 36 | # 2 15 37 | # 3 14 38 | # 5 15 39 | # 6 14 40 | # 7 15 41 | # 8 13 42 | # 9 15 43 | # 10 16 44 | # 11 14 45 | # 2017 1 14 46 | # 2 16 47 | # 3 14 48 | # 4 18 49 | # 5 18 50 | # 6 16 51 | # 7 16 52 | # 8 13 53 | # 9 17 54 | # 10 16 55 | # 11 18 56 | # 12 16 57 | # Name: duration, dtype: int64 58 | -------------------------------------------------------------------------------- /Ch06/06_03/stocks.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch06/06_03/stocks.h5 -------------------------------------------------------------------------------- /Ch06/challenge/bikes.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch06/challenge/bikes.db -------------------------------------------------------------------------------- /Ch06/challenge/ride_duration.py: -------------------------------------------------------------------------------- 1 | """Using "bikes.db", find the 5 bikes (using "bike_id") that has the biggest 2 | 90% quantile of ride duration in the first quarter of 2017. 3 | """ 4 | -------------------------------------------------------------------------------- /Ch06/solution/bikes.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch06/solution/bikes.db -------------------------------------------------------------------------------- /Ch06/solution/ride_duration.py: -------------------------------------------------------------------------------- 1 | """Using "bikes.db", find the 5 bikes (using "bike_id") that has the biggest 2 | 90% quantile of ride duration in the first quarter of 2017. 3 | """ 4 | import sqlite3 5 | 6 | import pandas as pd 7 | 8 | conn = sqlite3.connect('bikes.db') 9 | sql = ''' 10 | SELECT bike_id, duration 11 | FROM bike_rides 12 | WHERE year == 2017 AND month < 4 13 | ''' 14 | df = pd.read_sql(sql, conn) 15 | out = df.groupby('bike_id')['duration'].quantile(.9) 16 | print(out.sort_values(ascending=False)[:5]) 17 | -------------------------------------------------------------------------------- /Ch07/07_04/relu.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3 2 | 3 | def relu(double n): 4 | if n < 0: 5 | return 0 6 | return n 7 | -------------------------------------------------------------------------------- /Ch07/07_04/relu1.py: -------------------------------------------------------------------------------- 1 | def relu(n): 2 | if n < 0: 3 | return 0 4 | return n 5 | -------------------------------------------------------------------------------- /Ch07/07_05/build/lib.linux-x86_64-3.8/relu.cpython-38-x86_64-linux-gnu.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch07/07_05/build/lib.linux-x86_64-3.8/relu.cpython-38-x86_64-linux-gnu.so -------------------------------------------------------------------------------- /Ch07/07_05/build/temp.linux-x86_64-3.8/relu.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch07/07_05/build/temp.linux-x86_64-3.8/relu.o -------------------------------------------------------------------------------- /Ch07/07_05/relu.c: -------------------------------------------------------------------------------- 1 | /* Generated by Cython 0.29.21 */ 2 | 3 | /* BEGIN: Cython Metadata 4 | { 5 | "distutils": { 6 | "name": "relu", 7 | "sources": [ 8 | "relu.pyx" 9 | ] 10 | }, 11 | "module_name": "relu" 12 | } 13 | END: Cython Metadata */ 14 | 15 | #define PY_SSIZE_T_CLEAN 16 | #include "Python.h" 17 | #ifndef Py_PYTHON_H 18 | #error Python headers needed to compile C extensions, please install development version of Python. 19 | #elif PY_VERSION_HEX < 0x02060000 || (0x03000000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x03030000) 20 | #error Cython requires Python 2.6+ or Python 3.3+. 21 | #else 22 | #define CYTHON_ABI "0_29_21" 23 | #define CYTHON_HEX_VERSION 0x001D15F0 24 | #define CYTHON_FUTURE_DIVISION 0 25 | #include 26 | #ifndef offsetof 27 | #define offsetof(type, member) ( (size_t) & ((type*)0) -> member ) 28 | #endif 29 | #if !defined(WIN32) && !defined(MS_WINDOWS) 30 | #ifndef __stdcall 31 | #define __stdcall 32 | #endif 33 | #ifndef __cdecl 34 | #define __cdecl 35 | #endif 36 | #ifndef __fastcall 37 | #define __fastcall 38 | #endif 39 | #endif 40 | #ifndef DL_IMPORT 41 | #define DL_IMPORT(t) t 42 | #endif 43 | #ifndef DL_EXPORT 44 | #define DL_EXPORT(t) t 45 | #endif 46 | #define __PYX_COMMA , 47 | #ifndef HAVE_LONG_LONG 48 | #if PY_VERSION_HEX >= 0x02070000 49 | #define HAVE_LONG_LONG 50 | #endif 51 | #endif 52 | #ifndef PY_LONG_LONG 53 | #define PY_LONG_LONG LONG_LONG 54 | #endif 55 | #ifndef Py_HUGE_VAL 56 | #define Py_HUGE_VAL HUGE_VAL 57 | #endif 58 | #ifdef PYPY_VERSION 59 | #define CYTHON_COMPILING_IN_PYPY 1 60 | #define CYTHON_COMPILING_IN_PYSTON 0 61 | #define CYTHON_COMPILING_IN_CPYTHON 0 62 | #undef CYTHON_USE_TYPE_SLOTS 63 | #define CYTHON_USE_TYPE_SLOTS 0 64 | #undef CYTHON_USE_PYTYPE_LOOKUP 65 | #define CYTHON_USE_PYTYPE_LOOKUP 0 66 | #if PY_VERSION_HEX < 0x03050000 67 | #undef CYTHON_USE_ASYNC_SLOTS 68 | #define CYTHON_USE_ASYNC_SLOTS 0 69 | #elif !defined(CYTHON_USE_ASYNC_SLOTS) 70 | #define CYTHON_USE_ASYNC_SLOTS 1 71 | #endif 72 | #undef CYTHON_USE_PYLIST_INTERNALS 73 | #define CYTHON_USE_PYLIST_INTERNALS 0 74 | #undef CYTHON_USE_UNICODE_INTERNALS 75 | #define CYTHON_USE_UNICODE_INTERNALS 0 76 | #undef CYTHON_USE_UNICODE_WRITER 77 | #define CYTHON_USE_UNICODE_WRITER 0 78 | #undef CYTHON_USE_PYLONG_INTERNALS 79 | #define CYTHON_USE_PYLONG_INTERNALS 0 80 | #undef CYTHON_AVOID_BORROWED_REFS 81 | #define CYTHON_AVOID_BORROWED_REFS 1 82 | #undef CYTHON_ASSUME_SAFE_MACROS 83 | #define CYTHON_ASSUME_SAFE_MACROS 0 84 | #undef CYTHON_UNPACK_METHODS 85 | #define CYTHON_UNPACK_METHODS 0 86 | #undef CYTHON_FAST_THREAD_STATE 87 | #define CYTHON_FAST_THREAD_STATE 0 88 | #undef CYTHON_FAST_PYCALL 89 | #define CYTHON_FAST_PYCALL 0 90 | #undef CYTHON_PEP489_MULTI_PHASE_INIT 91 | #define CYTHON_PEP489_MULTI_PHASE_INIT 0 92 | #undef CYTHON_USE_TP_FINALIZE 93 | #define CYTHON_USE_TP_FINALIZE 0 94 | #undef CYTHON_USE_DICT_VERSIONS 95 | #define CYTHON_USE_DICT_VERSIONS 0 96 | #undef CYTHON_USE_EXC_INFO_STACK 97 | #define CYTHON_USE_EXC_INFO_STACK 0 98 | #elif defined(PYSTON_VERSION) 99 | #define CYTHON_COMPILING_IN_PYPY 0 100 | #define CYTHON_COMPILING_IN_PYSTON 1 101 | #define CYTHON_COMPILING_IN_CPYTHON 0 102 | #ifndef CYTHON_USE_TYPE_SLOTS 103 | #define CYTHON_USE_TYPE_SLOTS 1 104 | #endif 105 | #undef CYTHON_USE_PYTYPE_LOOKUP 106 | #define CYTHON_USE_PYTYPE_LOOKUP 0 107 | #undef CYTHON_USE_ASYNC_SLOTS 108 | #define CYTHON_USE_ASYNC_SLOTS 0 109 | #undef CYTHON_USE_PYLIST_INTERNALS 110 | #define CYTHON_USE_PYLIST_INTERNALS 0 111 | #ifndef CYTHON_USE_UNICODE_INTERNALS 112 | #define CYTHON_USE_UNICODE_INTERNALS 1 113 | #endif 114 | #undef CYTHON_USE_UNICODE_WRITER 115 | #define CYTHON_USE_UNICODE_WRITER 0 116 | #undef CYTHON_USE_PYLONG_INTERNALS 117 | #define CYTHON_USE_PYLONG_INTERNALS 0 118 | #ifndef CYTHON_AVOID_BORROWED_REFS 119 | #define CYTHON_AVOID_BORROWED_REFS 0 120 | #endif 121 | #ifndef CYTHON_ASSUME_SAFE_MACROS 122 | #define CYTHON_ASSUME_SAFE_MACROS 1 123 | #endif 124 | #ifndef CYTHON_UNPACK_METHODS 125 | #define CYTHON_UNPACK_METHODS 1 126 | #endif 127 | #undef CYTHON_FAST_THREAD_STATE 128 | #define CYTHON_FAST_THREAD_STATE 0 129 | #undef CYTHON_FAST_PYCALL 130 | #define CYTHON_FAST_PYCALL 0 131 | #undef CYTHON_PEP489_MULTI_PHASE_INIT 132 | #define CYTHON_PEP489_MULTI_PHASE_INIT 0 133 | #undef CYTHON_USE_TP_FINALIZE 134 | #define CYTHON_USE_TP_FINALIZE 0 135 | #undef CYTHON_USE_DICT_VERSIONS 136 | #define CYTHON_USE_DICT_VERSIONS 0 137 | #undef CYTHON_USE_EXC_INFO_STACK 138 | #define CYTHON_USE_EXC_INFO_STACK 0 139 | #else 140 | #define CYTHON_COMPILING_IN_PYPY 0 141 | #define CYTHON_COMPILING_IN_PYSTON 0 142 | #define CYTHON_COMPILING_IN_CPYTHON 1 143 | #ifndef CYTHON_USE_TYPE_SLOTS 144 | #define CYTHON_USE_TYPE_SLOTS 1 145 | #endif 146 | #if PY_VERSION_HEX < 0x02070000 147 | #undef CYTHON_USE_PYTYPE_LOOKUP 148 | #define CYTHON_USE_PYTYPE_LOOKUP 0 149 | #elif !defined(CYTHON_USE_PYTYPE_LOOKUP) 150 | #define CYTHON_USE_PYTYPE_LOOKUP 1 151 | #endif 152 | #if PY_MAJOR_VERSION < 3 153 | #undef CYTHON_USE_ASYNC_SLOTS 154 | #define CYTHON_USE_ASYNC_SLOTS 0 155 | #elif !defined(CYTHON_USE_ASYNC_SLOTS) 156 | #define CYTHON_USE_ASYNC_SLOTS 1 157 | #endif 158 | #if PY_VERSION_HEX < 0x02070000 159 | #undef CYTHON_USE_PYLONG_INTERNALS 160 | #define CYTHON_USE_PYLONG_INTERNALS 0 161 | #elif !defined(CYTHON_USE_PYLONG_INTERNALS) 162 | #define CYTHON_USE_PYLONG_INTERNALS 1 163 | #endif 164 | #ifndef CYTHON_USE_PYLIST_INTERNALS 165 | #define CYTHON_USE_PYLIST_INTERNALS 1 166 | #endif 167 | #ifndef CYTHON_USE_UNICODE_INTERNALS 168 | #define CYTHON_USE_UNICODE_INTERNALS 1 169 | #endif 170 | #if PY_VERSION_HEX < 0x030300F0 171 | #undef CYTHON_USE_UNICODE_WRITER 172 | #define CYTHON_USE_UNICODE_WRITER 0 173 | #elif !defined(CYTHON_USE_UNICODE_WRITER) 174 | #define CYTHON_USE_UNICODE_WRITER 1 175 | #endif 176 | #ifndef CYTHON_AVOID_BORROWED_REFS 177 | #define CYTHON_AVOID_BORROWED_REFS 0 178 | #endif 179 | #ifndef CYTHON_ASSUME_SAFE_MACROS 180 | #define CYTHON_ASSUME_SAFE_MACROS 1 181 | #endif 182 | #ifndef CYTHON_UNPACK_METHODS 183 | #define CYTHON_UNPACK_METHODS 1 184 | #endif 185 | #ifndef CYTHON_FAST_THREAD_STATE 186 | #define CYTHON_FAST_THREAD_STATE 1 187 | #endif 188 | #ifndef CYTHON_FAST_PYCALL 189 | #define CYTHON_FAST_PYCALL 1 190 | #endif 191 | #ifndef CYTHON_PEP489_MULTI_PHASE_INIT 192 | #define CYTHON_PEP489_MULTI_PHASE_INIT (PY_VERSION_HEX >= 0x03050000) 193 | #endif 194 | #ifndef CYTHON_USE_TP_FINALIZE 195 | #define CYTHON_USE_TP_FINALIZE (PY_VERSION_HEX >= 0x030400a1) 196 | #endif 197 | #ifndef CYTHON_USE_DICT_VERSIONS 198 | #define CYTHON_USE_DICT_VERSIONS (PY_VERSION_HEX >= 0x030600B1) 199 | #endif 200 | #ifndef CYTHON_USE_EXC_INFO_STACK 201 | #define CYTHON_USE_EXC_INFO_STACK (PY_VERSION_HEX >= 0x030700A3) 202 | #endif 203 | #endif 204 | #if !defined(CYTHON_FAST_PYCCALL) 205 | #define CYTHON_FAST_PYCCALL (CYTHON_FAST_PYCALL && PY_VERSION_HEX >= 0x030600B1) 206 | #endif 207 | #if CYTHON_USE_PYLONG_INTERNALS 208 | #include "longintrepr.h" 209 | #undef SHIFT 210 | #undef BASE 211 | #undef MASK 212 | #ifdef SIZEOF_VOID_P 213 | enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) }; 214 | #endif 215 | #endif 216 | #ifndef __has_attribute 217 | #define __has_attribute(x) 0 218 | #endif 219 | #ifndef __has_cpp_attribute 220 | #define __has_cpp_attribute(x) 0 221 | #endif 222 | #ifndef CYTHON_RESTRICT 223 | #if defined(__GNUC__) 224 | #define CYTHON_RESTRICT __restrict__ 225 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 226 | #define CYTHON_RESTRICT __restrict 227 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 228 | #define CYTHON_RESTRICT restrict 229 | #else 230 | #define CYTHON_RESTRICT 231 | #endif 232 | #endif 233 | #ifndef CYTHON_UNUSED 234 | # if defined(__GNUC__) 235 | # if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) 236 | # define CYTHON_UNUSED __attribute__ ((__unused__)) 237 | # else 238 | # define CYTHON_UNUSED 239 | # endif 240 | # elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER)) 241 | # define CYTHON_UNUSED __attribute__ ((__unused__)) 242 | # else 243 | # define CYTHON_UNUSED 244 | # endif 245 | #endif 246 | #ifndef CYTHON_MAYBE_UNUSED_VAR 247 | # if defined(__cplusplus) 248 | template void CYTHON_MAYBE_UNUSED_VAR( const T& ) { } 249 | # else 250 | # define CYTHON_MAYBE_UNUSED_VAR(x) (void)(x) 251 | # endif 252 | #endif 253 | #ifndef CYTHON_NCP_UNUSED 254 | # if CYTHON_COMPILING_IN_CPYTHON 255 | # define CYTHON_NCP_UNUSED 256 | # else 257 | # define CYTHON_NCP_UNUSED CYTHON_UNUSED 258 | # endif 259 | #endif 260 | #define __Pyx_void_to_None(void_result) ((void)(void_result), Py_INCREF(Py_None), Py_None) 261 | #ifdef _MSC_VER 262 | #ifndef _MSC_STDINT_H_ 263 | #if _MSC_VER < 1300 264 | typedef unsigned char uint8_t; 265 | typedef unsigned int uint32_t; 266 | #else 267 | typedef unsigned __int8 uint8_t; 268 | typedef unsigned __int32 uint32_t; 269 | #endif 270 | #endif 271 | #else 272 | #include 273 | #endif 274 | #ifndef CYTHON_FALLTHROUGH 275 | #if defined(__cplusplus) && __cplusplus >= 201103L 276 | #if __has_cpp_attribute(fallthrough) 277 | #define CYTHON_FALLTHROUGH [[fallthrough]] 278 | #elif __has_cpp_attribute(clang::fallthrough) 279 | #define CYTHON_FALLTHROUGH [[clang::fallthrough]] 280 | #elif __has_cpp_attribute(gnu::fallthrough) 281 | #define CYTHON_FALLTHROUGH [[gnu::fallthrough]] 282 | #endif 283 | #endif 284 | #ifndef CYTHON_FALLTHROUGH 285 | #if __has_attribute(fallthrough) 286 | #define CYTHON_FALLTHROUGH __attribute__((fallthrough)) 287 | #else 288 | #define CYTHON_FALLTHROUGH 289 | #endif 290 | #endif 291 | #if defined(__clang__ ) && defined(__apple_build_version__) 292 | #if __apple_build_version__ < 7000000 293 | #undef CYTHON_FALLTHROUGH 294 | #define CYTHON_FALLTHROUGH 295 | #endif 296 | #endif 297 | #endif 298 | 299 | #ifndef CYTHON_INLINE 300 | #if defined(__clang__) 301 | #define CYTHON_INLINE __inline__ __attribute__ ((__unused__)) 302 | #elif defined(__GNUC__) 303 | #define CYTHON_INLINE __inline__ 304 | #elif defined(_MSC_VER) 305 | #define CYTHON_INLINE __inline 306 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 307 | #define CYTHON_INLINE inline 308 | #else 309 | #define CYTHON_INLINE 310 | #endif 311 | #endif 312 | 313 | #if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag) 314 | #define Py_OptimizeFlag 0 315 | #endif 316 | #define __PYX_BUILD_PY_SSIZE_T "n" 317 | #define CYTHON_FORMAT_SSIZE_T "z" 318 | #if PY_MAJOR_VERSION < 3 319 | #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" 320 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ 321 | PyCode_New(a+k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 322 | #define __Pyx_DefaultClassType PyClass_Type 323 | #else 324 | #define __Pyx_BUILTIN_MODULE_NAME "builtins" 325 | #if PY_VERSION_HEX >= 0x030800A4 && PY_VERSION_HEX < 0x030800B2 326 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ 327 | PyCode_New(a, 0, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 328 | #else 329 | #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ 330 | PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) 331 | #endif 332 | #define __Pyx_DefaultClassType PyType_Type 333 | #endif 334 | #ifndef Py_TPFLAGS_CHECKTYPES 335 | #define Py_TPFLAGS_CHECKTYPES 0 336 | #endif 337 | #ifndef Py_TPFLAGS_HAVE_INDEX 338 | #define Py_TPFLAGS_HAVE_INDEX 0 339 | #endif 340 | #ifndef Py_TPFLAGS_HAVE_NEWBUFFER 341 | #define Py_TPFLAGS_HAVE_NEWBUFFER 0 342 | #endif 343 | #ifndef Py_TPFLAGS_HAVE_FINALIZE 344 | #define Py_TPFLAGS_HAVE_FINALIZE 0 345 | #endif 346 | #ifndef METH_STACKLESS 347 | #define METH_STACKLESS 0 348 | #endif 349 | #if PY_VERSION_HEX <= 0x030700A3 || !defined(METH_FASTCALL) 350 | #ifndef METH_FASTCALL 351 | #define METH_FASTCALL 0x80 352 | #endif 353 | typedef PyObject *(*__Pyx_PyCFunctionFast) (PyObject *self, PyObject *const *args, Py_ssize_t nargs); 354 | typedef PyObject *(*__Pyx_PyCFunctionFastWithKeywords) (PyObject *self, PyObject *const *args, 355 | Py_ssize_t nargs, PyObject *kwnames); 356 | #else 357 | #define __Pyx_PyCFunctionFast _PyCFunctionFast 358 | #define __Pyx_PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords 359 | #endif 360 | #if CYTHON_FAST_PYCCALL 361 | #define __Pyx_PyFastCFunction_Check(func)\ 362 | ((PyCFunction_Check(func) && (METH_FASTCALL == (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS))))) 363 | #else 364 | #define __Pyx_PyFastCFunction_Check(func) 0 365 | #endif 366 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc) 367 | #define PyObject_Malloc(s) PyMem_Malloc(s) 368 | #define PyObject_Free(p) PyMem_Free(p) 369 | #define PyObject_Realloc(p) PyMem_Realloc(p) 370 | #endif 371 | #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x030400A1 372 | #define PyMem_RawMalloc(n) PyMem_Malloc(n) 373 | #define PyMem_RawRealloc(p, n) PyMem_Realloc(p, n) 374 | #define PyMem_RawFree(p) PyMem_Free(p) 375 | #endif 376 | #if CYTHON_COMPILING_IN_PYSTON 377 | #define __Pyx_PyCode_HasFreeVars(co) PyCode_HasFreeVars(co) 378 | #define __Pyx_PyFrame_SetLineNumber(frame, lineno) PyFrame_SetLineNumber(frame, lineno) 379 | #else 380 | #define __Pyx_PyCode_HasFreeVars(co) (PyCode_GetNumFree(co) > 0) 381 | #define __Pyx_PyFrame_SetLineNumber(frame, lineno) (frame)->f_lineno = (lineno) 382 | #endif 383 | #if !CYTHON_FAST_THREAD_STATE || PY_VERSION_HEX < 0x02070000 384 | #define __Pyx_PyThreadState_Current PyThreadState_GET() 385 | #elif PY_VERSION_HEX >= 0x03060000 386 | #define __Pyx_PyThreadState_Current _PyThreadState_UncheckedGet() 387 | #elif PY_VERSION_HEX >= 0x03000000 388 | #define __Pyx_PyThreadState_Current PyThreadState_GET() 389 | #else 390 | #define __Pyx_PyThreadState_Current _PyThreadState_Current 391 | #endif 392 | #if PY_VERSION_HEX < 0x030700A2 && !defined(PyThread_tss_create) && !defined(Py_tss_NEEDS_INIT) 393 | #include "pythread.h" 394 | #define Py_tss_NEEDS_INIT 0 395 | typedef int Py_tss_t; 396 | static CYTHON_INLINE int PyThread_tss_create(Py_tss_t *key) { 397 | *key = PyThread_create_key(); 398 | return 0; 399 | } 400 | static CYTHON_INLINE Py_tss_t * PyThread_tss_alloc(void) { 401 | Py_tss_t *key = (Py_tss_t *)PyObject_Malloc(sizeof(Py_tss_t)); 402 | *key = Py_tss_NEEDS_INIT; 403 | return key; 404 | } 405 | static CYTHON_INLINE void PyThread_tss_free(Py_tss_t *key) { 406 | PyObject_Free(key); 407 | } 408 | static CYTHON_INLINE int PyThread_tss_is_created(Py_tss_t *key) { 409 | return *key != Py_tss_NEEDS_INIT; 410 | } 411 | static CYTHON_INLINE void PyThread_tss_delete(Py_tss_t *key) { 412 | PyThread_delete_key(*key); 413 | *key = Py_tss_NEEDS_INIT; 414 | } 415 | static CYTHON_INLINE int PyThread_tss_set(Py_tss_t *key, void *value) { 416 | return PyThread_set_key_value(*key, value); 417 | } 418 | static CYTHON_INLINE void * PyThread_tss_get(Py_tss_t *key) { 419 | return PyThread_get_key_value(*key); 420 | } 421 | #endif 422 | #if CYTHON_COMPILING_IN_CPYTHON || defined(_PyDict_NewPresized) 423 | #define __Pyx_PyDict_NewPresized(n) ((n <= 8) ? PyDict_New() : _PyDict_NewPresized(n)) 424 | #else 425 | #define __Pyx_PyDict_NewPresized(n) PyDict_New() 426 | #endif 427 | #if PY_MAJOR_VERSION >= 3 || CYTHON_FUTURE_DIVISION 428 | #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) 429 | #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) 430 | #else 431 | #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y) 432 | #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y) 433 | #endif 434 | #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030500A1 && CYTHON_USE_UNICODE_INTERNALS 435 | #define __Pyx_PyDict_GetItemStr(dict, name) _PyDict_GetItem_KnownHash(dict, name, ((PyASCIIObject *) name)->hash) 436 | #else 437 | #define __Pyx_PyDict_GetItemStr(dict, name) PyDict_GetItem(dict, name) 438 | #endif 439 | #if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND) 440 | #define CYTHON_PEP393_ENABLED 1 441 | #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ?\ 442 | 0 : _PyUnicode_Ready((PyObject *)(op))) 443 | #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u) 444 | #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i) 445 | #define __Pyx_PyUnicode_MAX_CHAR_VALUE(u) PyUnicode_MAX_CHAR_VALUE(u) 446 | #define __Pyx_PyUnicode_KIND(u) PyUnicode_KIND(u) 447 | #define __Pyx_PyUnicode_DATA(u) PyUnicode_DATA(u) 448 | #define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i) 449 | #define __Pyx_PyUnicode_WRITE(k, d, i, ch) PyUnicode_WRITE(k, d, i, ch) 450 | #if defined(PyUnicode_IS_READY) && defined(PyUnicode_GET_SIZE) 451 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : PyUnicode_GET_SIZE(u))) 452 | #else 453 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_LENGTH(u)) 454 | #endif 455 | #else 456 | #define CYTHON_PEP393_ENABLED 0 457 | #define PyUnicode_1BYTE_KIND 1 458 | #define PyUnicode_2BYTE_KIND 2 459 | #define PyUnicode_4BYTE_KIND 4 460 | #define __Pyx_PyUnicode_READY(op) (0) 461 | #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u) 462 | #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i])) 463 | #define __Pyx_PyUnicode_MAX_CHAR_VALUE(u) ((sizeof(Py_UNICODE) == 2) ? 65535 : 1114111) 464 | #define __Pyx_PyUnicode_KIND(u) (sizeof(Py_UNICODE)) 465 | #define __Pyx_PyUnicode_DATA(u) ((void*)PyUnicode_AS_UNICODE(u)) 466 | #define __Pyx_PyUnicode_READ(k, d, i) ((void)(k), (Py_UCS4)(((Py_UNICODE*)d)[i])) 467 | #define __Pyx_PyUnicode_WRITE(k, d, i, ch) (((void)(k)), ((Py_UNICODE*)d)[i] = ch) 468 | #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_SIZE(u)) 469 | #endif 470 | #if CYTHON_COMPILING_IN_PYPY 471 | #define __Pyx_PyUnicode_Concat(a, b) PyNumber_Add(a, b) 472 | #define __Pyx_PyUnicode_ConcatSafe(a, b) PyNumber_Add(a, b) 473 | #else 474 | #define __Pyx_PyUnicode_Concat(a, b) PyUnicode_Concat(a, b) 475 | #define __Pyx_PyUnicode_ConcatSafe(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ?\ 476 | PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b)) 477 | #endif 478 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyUnicode_Contains) 479 | #define PyUnicode_Contains(u, s) PySequence_Contains(u, s) 480 | #endif 481 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyByteArray_Check) 482 | #define PyByteArray_Check(obj) PyObject_TypeCheck(obj, &PyByteArray_Type) 483 | #endif 484 | #if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Format) 485 | #define PyObject_Format(obj, fmt) PyObject_CallMethod(obj, "__format__", "O", fmt) 486 | #endif 487 | #define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None || (PyString_Check(b) && !PyString_CheckExact(b)))) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b)) 488 | #define __Pyx_PyUnicode_FormatSafe(a, b) ((unlikely((a) == Py_None || (PyUnicode_Check(b) && !PyUnicode_CheckExact(b)))) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b)) 489 | #if PY_MAJOR_VERSION >= 3 490 | #define __Pyx_PyString_Format(a, b) PyUnicode_Format(a, b) 491 | #else 492 | #define __Pyx_PyString_Format(a, b) PyString_Format(a, b) 493 | #endif 494 | #if PY_MAJOR_VERSION < 3 && !defined(PyObject_ASCII) 495 | #define PyObject_ASCII(o) PyObject_Repr(o) 496 | #endif 497 | #if PY_MAJOR_VERSION >= 3 498 | #define PyBaseString_Type PyUnicode_Type 499 | #define PyStringObject PyUnicodeObject 500 | #define PyString_Type PyUnicode_Type 501 | #define PyString_Check PyUnicode_Check 502 | #define PyString_CheckExact PyUnicode_CheckExact 503 | #ifndef PyObject_Unicode 504 | #define PyObject_Unicode PyObject_Str 505 | #endif 506 | #endif 507 | #if PY_MAJOR_VERSION >= 3 508 | #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj) 509 | #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj) 510 | #else 511 | #define __Pyx_PyBaseString_Check(obj) (PyString_Check(obj) || PyUnicode_Check(obj)) 512 | #define __Pyx_PyBaseString_CheckExact(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj)) 513 | #endif 514 | #ifndef PySet_CheckExact 515 | #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) 516 | #endif 517 | #if PY_VERSION_HEX >= 0x030900A4 518 | #define __Pyx_SET_REFCNT(obj, refcnt) Py_SET_REFCNT(obj, refcnt) 519 | #define __Pyx_SET_SIZE(obj, size) Py_SET_SIZE(obj, size) 520 | #else 521 | #define __Pyx_SET_REFCNT(obj, refcnt) Py_REFCNT(obj) = (refcnt) 522 | #define __Pyx_SET_SIZE(obj, size) Py_SIZE(obj) = (size) 523 | #endif 524 | #if CYTHON_ASSUME_SAFE_MACROS 525 | #define __Pyx_PySequence_SIZE(seq) Py_SIZE(seq) 526 | #else 527 | #define __Pyx_PySequence_SIZE(seq) PySequence_Size(seq) 528 | #endif 529 | #if PY_MAJOR_VERSION >= 3 530 | #define PyIntObject PyLongObject 531 | #define PyInt_Type PyLong_Type 532 | #define PyInt_Check(op) PyLong_Check(op) 533 | #define PyInt_CheckExact(op) PyLong_CheckExact(op) 534 | #define PyInt_FromString PyLong_FromString 535 | #define PyInt_FromUnicode PyLong_FromUnicode 536 | #define PyInt_FromLong PyLong_FromLong 537 | #define PyInt_FromSize_t PyLong_FromSize_t 538 | #define PyInt_FromSsize_t PyLong_FromSsize_t 539 | #define PyInt_AsLong PyLong_AsLong 540 | #define PyInt_AS_LONG PyLong_AS_LONG 541 | #define PyInt_AsSsize_t PyLong_AsSsize_t 542 | #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask 543 | #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask 544 | #define PyNumber_Int PyNumber_Long 545 | #endif 546 | #if PY_MAJOR_VERSION >= 3 547 | #define PyBoolObject PyLongObject 548 | #endif 549 | #if PY_MAJOR_VERSION >= 3 && CYTHON_COMPILING_IN_PYPY 550 | #ifndef PyUnicode_InternFromString 551 | #define PyUnicode_InternFromString(s) PyUnicode_FromString(s) 552 | #endif 553 | #endif 554 | #if PY_VERSION_HEX < 0x030200A4 555 | typedef long Py_hash_t; 556 | #define __Pyx_PyInt_FromHash_t PyInt_FromLong 557 | #define __Pyx_PyInt_AsHash_t PyInt_AsLong 558 | #else 559 | #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t 560 | #define __Pyx_PyInt_AsHash_t PyInt_AsSsize_t 561 | #endif 562 | #if PY_MAJOR_VERSION >= 3 563 | #define __Pyx_PyMethod_New(func, self, klass) ((self) ? ((void)(klass), PyMethod_New(func, self)) : __Pyx_NewRef(func)) 564 | #else 565 | #define __Pyx_PyMethod_New(func, self, klass) PyMethod_New(func, self, klass) 566 | #endif 567 | #if CYTHON_USE_ASYNC_SLOTS 568 | #if PY_VERSION_HEX >= 0x030500B1 569 | #define __Pyx_PyAsyncMethodsStruct PyAsyncMethods 570 | #define __Pyx_PyType_AsAsync(obj) (Py_TYPE(obj)->tp_as_async) 571 | #else 572 | #define __Pyx_PyType_AsAsync(obj) ((__Pyx_PyAsyncMethodsStruct*) (Py_TYPE(obj)->tp_reserved)) 573 | #endif 574 | #else 575 | #define __Pyx_PyType_AsAsync(obj) NULL 576 | #endif 577 | #ifndef __Pyx_PyAsyncMethodsStruct 578 | typedef struct { 579 | unaryfunc am_await; 580 | unaryfunc am_aiter; 581 | unaryfunc am_anext; 582 | } __Pyx_PyAsyncMethodsStruct; 583 | #endif 584 | 585 | #if defined(WIN32) || defined(MS_WINDOWS) 586 | #define _USE_MATH_DEFINES 587 | #endif 588 | #include 589 | #ifdef NAN 590 | #define __PYX_NAN() ((float) NAN) 591 | #else 592 | static CYTHON_INLINE float __PYX_NAN() { 593 | float value; 594 | memset(&value, 0xFF, sizeof(value)); 595 | return value; 596 | } 597 | #endif 598 | #if defined(__CYGWIN__) && defined(_LDBL_EQ_DBL) 599 | #define __Pyx_truncl trunc 600 | #else 601 | #define __Pyx_truncl truncl 602 | #endif 603 | 604 | #define __PYX_MARK_ERR_POS(f_index, lineno) \ 605 | { __pyx_filename = __pyx_f[f_index]; (void)__pyx_filename; __pyx_lineno = lineno; (void)__pyx_lineno; __pyx_clineno = __LINE__; (void)__pyx_clineno; } 606 | #define __PYX_ERR(f_index, lineno, Ln_error) \ 607 | { __PYX_MARK_ERR_POS(f_index, lineno) goto Ln_error; } 608 | 609 | #ifndef __PYX_EXTERN_C 610 | #ifdef __cplusplus 611 | #define __PYX_EXTERN_C extern "C" 612 | #else 613 | #define __PYX_EXTERN_C extern 614 | #endif 615 | #endif 616 | 617 | #define __PYX_HAVE__relu 618 | #define __PYX_HAVE_API__relu 619 | /* Early includes */ 620 | #ifdef _OPENMP 621 | #include 622 | #endif /* _OPENMP */ 623 | 624 | #if defined(PYREX_WITHOUT_ASSERTIONS) && !defined(CYTHON_WITHOUT_ASSERTIONS) 625 | #define CYTHON_WITHOUT_ASSERTIONS 626 | #endif 627 | 628 | typedef struct {PyObject **p; const char *s; const Py_ssize_t n; const char* encoding; 629 | const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; 630 | 631 | #define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0 632 | #define __PYX_DEFAULT_STRING_ENCODING_IS_UTF8 0 633 | #define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT (PY_MAJOR_VERSION >= 3 && __PYX_DEFAULT_STRING_ENCODING_IS_UTF8) 634 | #define __PYX_DEFAULT_STRING_ENCODING "" 635 | #define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString 636 | #define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize 637 | #define __Pyx_uchar_cast(c) ((unsigned char)c) 638 | #define __Pyx_long_cast(x) ((long)x) 639 | #define __Pyx_fits_Py_ssize_t(v, type, is_signed) (\ 640 | (sizeof(type) < sizeof(Py_ssize_t)) ||\ 641 | (sizeof(type) > sizeof(Py_ssize_t) &&\ 642 | likely(v < (type)PY_SSIZE_T_MAX ||\ 643 | v == (type)PY_SSIZE_T_MAX) &&\ 644 | (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||\ 645 | v == (type)PY_SSIZE_T_MIN))) ||\ 646 | (sizeof(type) == sizeof(Py_ssize_t) &&\ 647 | (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||\ 648 | v == (type)PY_SSIZE_T_MAX))) ) 649 | static CYTHON_INLINE int __Pyx_is_valid_index(Py_ssize_t i, Py_ssize_t limit) { 650 | return (size_t) i < (size_t) limit; 651 | } 652 | #if defined (__cplusplus) && __cplusplus >= 201103L 653 | #include 654 | #define __Pyx_sst_abs(value) std::abs(value) 655 | #elif SIZEOF_INT >= SIZEOF_SIZE_T 656 | #define __Pyx_sst_abs(value) abs(value) 657 | #elif SIZEOF_LONG >= SIZEOF_SIZE_T 658 | #define __Pyx_sst_abs(value) labs(value) 659 | #elif defined (_MSC_VER) 660 | #define __Pyx_sst_abs(value) ((Py_ssize_t)_abs64(value)) 661 | #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L 662 | #define __Pyx_sst_abs(value) llabs(value) 663 | #elif defined (__GNUC__) 664 | #define __Pyx_sst_abs(value) __builtin_llabs(value) 665 | #else 666 | #define __Pyx_sst_abs(value) ((value<0) ? -value : value) 667 | #endif 668 | static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject*); 669 | static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length); 670 | #define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s)) 671 | #define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l) 672 | #define __Pyx_PyBytes_FromString PyBytes_FromString 673 | #define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize 674 | static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*); 675 | #if PY_MAJOR_VERSION < 3 676 | #define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString 677 | #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize 678 | #else 679 | #define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString 680 | #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize 681 | #endif 682 | #define __Pyx_PyBytes_AsWritableString(s) ((char*) PyBytes_AS_STRING(s)) 683 | #define __Pyx_PyBytes_AsWritableSString(s) ((signed char*) PyBytes_AS_STRING(s)) 684 | #define __Pyx_PyBytes_AsWritableUString(s) ((unsigned char*) PyBytes_AS_STRING(s)) 685 | #define __Pyx_PyBytes_AsString(s) ((const char*) PyBytes_AS_STRING(s)) 686 | #define __Pyx_PyBytes_AsSString(s) ((const signed char*) PyBytes_AS_STRING(s)) 687 | #define __Pyx_PyBytes_AsUString(s) ((const unsigned char*) PyBytes_AS_STRING(s)) 688 | #define __Pyx_PyObject_AsWritableString(s) ((char*) __Pyx_PyObject_AsString(s)) 689 | #define __Pyx_PyObject_AsWritableSString(s) ((signed char*) __Pyx_PyObject_AsString(s)) 690 | #define __Pyx_PyObject_AsWritableUString(s) ((unsigned char*) __Pyx_PyObject_AsString(s)) 691 | #define __Pyx_PyObject_AsSString(s) ((const signed char*) __Pyx_PyObject_AsString(s)) 692 | #define __Pyx_PyObject_AsUString(s) ((const unsigned char*) __Pyx_PyObject_AsString(s)) 693 | #define __Pyx_PyObject_FromCString(s) __Pyx_PyObject_FromString((const char*)s) 694 | #define __Pyx_PyBytes_FromCString(s) __Pyx_PyBytes_FromString((const char*)s) 695 | #define __Pyx_PyByteArray_FromCString(s) __Pyx_PyByteArray_FromString((const char*)s) 696 | #define __Pyx_PyStr_FromCString(s) __Pyx_PyStr_FromString((const char*)s) 697 | #define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s) 698 | static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) { 699 | const Py_UNICODE *u_end = u; 700 | while (*u_end++) ; 701 | return (size_t)(u_end - u - 1); 702 | } 703 | #define __Pyx_PyUnicode_FromUnicode(u) PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u)) 704 | #define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode 705 | #define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode 706 | #define __Pyx_NewRef(obj) (Py_INCREF(obj), obj) 707 | #define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None) 708 | static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b); 709 | static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); 710 | static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject*); 711 | static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x); 712 | #define __Pyx_PySequence_Tuple(obj)\ 713 | (likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj)) 714 | static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); 715 | static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); 716 | #if CYTHON_ASSUME_SAFE_MACROS 717 | #define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) 718 | #else 719 | #define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x) 720 | #endif 721 | #define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x)) 722 | #if PY_MAJOR_VERSION >= 3 723 | #define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x)) 724 | #else 725 | #define __Pyx_PyNumber_Int(x) (PyInt_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Int(x)) 726 | #endif 727 | #define __Pyx_PyNumber_Float(x) (PyFloat_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Float(x)) 728 | #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 729 | static int __Pyx_sys_getdefaultencoding_not_ascii; 730 | static int __Pyx_init_sys_getdefaultencoding_params(void) { 731 | PyObject* sys; 732 | PyObject* default_encoding = NULL; 733 | PyObject* ascii_chars_u = NULL; 734 | PyObject* ascii_chars_b = NULL; 735 | const char* default_encoding_c; 736 | sys = PyImport_ImportModule("sys"); 737 | if (!sys) goto bad; 738 | default_encoding = PyObject_CallMethod(sys, (char*) "getdefaultencoding", NULL); 739 | Py_DECREF(sys); 740 | if (!default_encoding) goto bad; 741 | default_encoding_c = PyBytes_AsString(default_encoding); 742 | if (!default_encoding_c) goto bad; 743 | if (strcmp(default_encoding_c, "ascii") == 0) { 744 | __Pyx_sys_getdefaultencoding_not_ascii = 0; 745 | } else { 746 | char ascii_chars[128]; 747 | int c; 748 | for (c = 0; c < 128; c++) { 749 | ascii_chars[c] = c; 750 | } 751 | __Pyx_sys_getdefaultencoding_not_ascii = 1; 752 | ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL); 753 | if (!ascii_chars_u) goto bad; 754 | ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL); 755 | if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) { 756 | PyErr_Format( 757 | PyExc_ValueError, 758 | "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.", 759 | default_encoding_c); 760 | goto bad; 761 | } 762 | Py_DECREF(ascii_chars_u); 763 | Py_DECREF(ascii_chars_b); 764 | } 765 | Py_DECREF(default_encoding); 766 | return 0; 767 | bad: 768 | Py_XDECREF(default_encoding); 769 | Py_XDECREF(ascii_chars_u); 770 | Py_XDECREF(ascii_chars_b); 771 | return -1; 772 | } 773 | #endif 774 | #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3 775 | #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL) 776 | #else 777 | #define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL) 778 | #if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 779 | static char* __PYX_DEFAULT_STRING_ENCODING; 780 | static int __Pyx_init_sys_getdefaultencoding_params(void) { 781 | PyObject* sys; 782 | PyObject* default_encoding = NULL; 783 | char* default_encoding_c; 784 | sys = PyImport_ImportModule("sys"); 785 | if (!sys) goto bad; 786 | default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); 787 | Py_DECREF(sys); 788 | if (!default_encoding) goto bad; 789 | default_encoding_c = PyBytes_AsString(default_encoding); 790 | if (!default_encoding_c) goto bad; 791 | __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c) + 1); 792 | if (!__PYX_DEFAULT_STRING_ENCODING) goto bad; 793 | strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c); 794 | Py_DECREF(default_encoding); 795 | return 0; 796 | bad: 797 | Py_XDECREF(default_encoding); 798 | return -1; 799 | } 800 | #endif 801 | #endif 802 | 803 | 804 | /* Test for GCC > 2.95 */ 805 | #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))) 806 | #define likely(x) __builtin_expect(!!(x), 1) 807 | #define unlikely(x) __builtin_expect(!!(x), 0) 808 | #else /* !__GNUC__ or GCC < 2.95 */ 809 | #define likely(x) (x) 810 | #define unlikely(x) (x) 811 | #endif /* __GNUC__ */ 812 | static CYTHON_INLINE void __Pyx_pretend_to_initialize(void* ptr) { (void)ptr; } 813 | 814 | static PyObject *__pyx_m = NULL; 815 | static PyObject *__pyx_d; 816 | static PyObject *__pyx_b; 817 | static PyObject *__pyx_cython_runtime = NULL; 818 | static PyObject *__pyx_empty_tuple; 819 | static PyObject *__pyx_empty_bytes; 820 | static PyObject *__pyx_empty_unicode; 821 | static int __pyx_lineno; 822 | static int __pyx_clineno = 0; 823 | static const char * __pyx_cfilenm= __FILE__; 824 | static const char *__pyx_filename; 825 | 826 | 827 | static const char *__pyx_f[] = { 828 | "relu.pyx", 829 | }; 830 | 831 | /*--- Type declarations ---*/ 832 | 833 | /* --- Runtime support code (head) --- */ 834 | /* Refnanny.proto */ 835 | #ifndef CYTHON_REFNANNY 836 | #define CYTHON_REFNANNY 0 837 | #endif 838 | #if CYTHON_REFNANNY 839 | typedef struct { 840 | void (*INCREF)(void*, PyObject*, int); 841 | void (*DECREF)(void*, PyObject*, int); 842 | void (*GOTREF)(void*, PyObject*, int); 843 | void (*GIVEREF)(void*, PyObject*, int); 844 | void* (*SetupContext)(const char*, int, const char*); 845 | void (*FinishContext)(void**); 846 | } __Pyx_RefNannyAPIStruct; 847 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL; 848 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); 849 | #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL; 850 | #ifdef WITH_THREAD 851 | #define __Pyx_RefNannySetupContext(name, acquire_gil)\ 852 | if (acquire_gil) {\ 853 | PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();\ 854 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ 855 | PyGILState_Release(__pyx_gilstate_save);\ 856 | } else {\ 857 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ 858 | } 859 | #else 860 | #define __Pyx_RefNannySetupContext(name, acquire_gil)\ 861 | __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__) 862 | #endif 863 | #define __Pyx_RefNannyFinishContext()\ 864 | __Pyx_RefNanny->FinishContext(&__pyx_refnanny) 865 | #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 866 | #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 867 | #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 868 | #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__) 869 | #define __Pyx_XINCREF(r) do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0) 870 | #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0) 871 | #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0) 872 | #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0) 873 | #else 874 | #define __Pyx_RefNannyDeclarations 875 | #define __Pyx_RefNannySetupContext(name, acquire_gil) 876 | #define __Pyx_RefNannyFinishContext() 877 | #define __Pyx_INCREF(r) Py_INCREF(r) 878 | #define __Pyx_DECREF(r) Py_DECREF(r) 879 | #define __Pyx_GOTREF(r) 880 | #define __Pyx_GIVEREF(r) 881 | #define __Pyx_XINCREF(r) Py_XINCREF(r) 882 | #define __Pyx_XDECREF(r) Py_XDECREF(r) 883 | #define __Pyx_XGOTREF(r) 884 | #define __Pyx_XGIVEREF(r) 885 | #endif 886 | #define __Pyx_XDECREF_SET(r, v) do {\ 887 | PyObject *tmp = (PyObject *) r;\ 888 | r = v; __Pyx_XDECREF(tmp);\ 889 | } while (0) 890 | #define __Pyx_DECREF_SET(r, v) do {\ 891 | PyObject *tmp = (PyObject *) r;\ 892 | r = v; __Pyx_DECREF(tmp);\ 893 | } while (0) 894 | #define __Pyx_CLEAR(r) do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0) 895 | #define __Pyx_XCLEAR(r) do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0) 896 | 897 | /* PyDictVersioning.proto */ 898 | #if CYTHON_USE_DICT_VERSIONS && CYTHON_USE_TYPE_SLOTS 899 | #define __PYX_DICT_VERSION_INIT ((PY_UINT64_T) -1) 900 | #define __PYX_GET_DICT_VERSION(dict) (((PyDictObject*)(dict))->ma_version_tag) 901 | #define __PYX_UPDATE_DICT_CACHE(dict, value, cache_var, version_var)\ 902 | (version_var) = __PYX_GET_DICT_VERSION(dict);\ 903 | (cache_var) = (value); 904 | #define __PYX_PY_DICT_LOOKUP_IF_MODIFIED(VAR, DICT, LOOKUP) {\ 905 | static PY_UINT64_T __pyx_dict_version = 0;\ 906 | static PyObject *__pyx_dict_cached_value = NULL;\ 907 | if (likely(__PYX_GET_DICT_VERSION(DICT) == __pyx_dict_version)) {\ 908 | (VAR) = __pyx_dict_cached_value;\ 909 | } else {\ 910 | (VAR) = __pyx_dict_cached_value = (LOOKUP);\ 911 | __pyx_dict_version = __PYX_GET_DICT_VERSION(DICT);\ 912 | }\ 913 | } 914 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_tp_dict_version(PyObject *obj); 915 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_object_dict_version(PyObject *obj); 916 | static CYTHON_INLINE int __Pyx_object_dict_version_matches(PyObject* obj, PY_UINT64_T tp_dict_version, PY_UINT64_T obj_dict_version); 917 | #else 918 | #define __PYX_GET_DICT_VERSION(dict) (0) 919 | #define __PYX_UPDATE_DICT_CACHE(dict, value, cache_var, version_var) 920 | #define __PYX_PY_DICT_LOOKUP_IF_MODIFIED(VAR, DICT, LOOKUP) (VAR) = (LOOKUP); 921 | #endif 922 | 923 | /* PyObjectGetAttrStr.proto */ 924 | #if CYTHON_USE_TYPE_SLOTS 925 | static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name); 926 | #else 927 | #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n) 928 | #endif 929 | 930 | /* PyThreadStateGet.proto */ 931 | #if CYTHON_FAST_THREAD_STATE 932 | #define __Pyx_PyThreadState_declare PyThreadState *__pyx_tstate; 933 | #define __Pyx_PyThreadState_assign __pyx_tstate = __Pyx_PyThreadState_Current; 934 | #define __Pyx_PyErr_Occurred() __pyx_tstate->curexc_type 935 | #else 936 | #define __Pyx_PyThreadState_declare 937 | #define __Pyx_PyThreadState_assign 938 | #define __Pyx_PyErr_Occurred() PyErr_Occurred() 939 | #endif 940 | 941 | /* PyErrFetchRestore.proto */ 942 | #if CYTHON_FAST_THREAD_STATE 943 | #define __Pyx_PyErr_Clear() __Pyx_ErrRestore(NULL, NULL, NULL) 944 | #define __Pyx_ErrRestoreWithState(type, value, tb) __Pyx_ErrRestoreInState(PyThreadState_GET(), type, value, tb) 945 | #define __Pyx_ErrFetchWithState(type, value, tb) __Pyx_ErrFetchInState(PyThreadState_GET(), type, value, tb) 946 | #define __Pyx_ErrRestore(type, value, tb) __Pyx_ErrRestoreInState(__pyx_tstate, type, value, tb) 947 | #define __Pyx_ErrFetch(type, value, tb) __Pyx_ErrFetchInState(__pyx_tstate, type, value, tb) 948 | static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); 949 | static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); 950 | #if CYTHON_COMPILING_IN_CPYTHON 951 | #define __Pyx_PyErr_SetNone(exc) (Py_INCREF(exc), __Pyx_ErrRestore((exc), NULL, NULL)) 952 | #else 953 | #define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc) 954 | #endif 955 | #else 956 | #define __Pyx_PyErr_Clear() PyErr_Clear() 957 | #define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc) 958 | #define __Pyx_ErrRestoreWithState(type, value, tb) PyErr_Restore(type, value, tb) 959 | #define __Pyx_ErrFetchWithState(type, value, tb) PyErr_Fetch(type, value, tb) 960 | #define __Pyx_ErrRestoreInState(tstate, type, value, tb) PyErr_Restore(type, value, tb) 961 | #define __Pyx_ErrFetchInState(tstate, type, value, tb) PyErr_Fetch(type, value, tb) 962 | #define __Pyx_ErrRestore(type, value, tb) PyErr_Restore(type, value, tb) 963 | #define __Pyx_ErrFetch(type, value, tb) PyErr_Fetch(type, value, tb) 964 | #endif 965 | 966 | /* CLineInTraceback.proto */ 967 | #ifdef CYTHON_CLINE_IN_TRACEBACK 968 | #define __Pyx_CLineForTraceback(tstate, c_line) (((CYTHON_CLINE_IN_TRACEBACK)) ? c_line : 0) 969 | #else 970 | static int __Pyx_CLineForTraceback(PyThreadState *tstate, int c_line); 971 | #endif 972 | 973 | /* CodeObjectCache.proto */ 974 | typedef struct { 975 | PyCodeObject* code_object; 976 | int code_line; 977 | } __Pyx_CodeObjectCacheEntry; 978 | struct __Pyx_CodeObjectCache { 979 | int count; 980 | int max_count; 981 | __Pyx_CodeObjectCacheEntry* entries; 982 | }; 983 | static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL}; 984 | static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line); 985 | static PyCodeObject *__pyx_find_code_object(int code_line); 986 | static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object); 987 | 988 | /* AddTraceback.proto */ 989 | static void __Pyx_AddTraceback(const char *funcname, int c_line, 990 | int py_line, const char *filename); 991 | 992 | /* CIntToPy.proto */ 993 | static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value); 994 | 995 | /* CIntFromPy.proto */ 996 | static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *); 997 | 998 | /* CIntFromPy.proto */ 999 | static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *); 1000 | 1001 | /* FastTypeChecks.proto */ 1002 | #if CYTHON_COMPILING_IN_CPYTHON 1003 | #define __Pyx_TypeCheck(obj, type) __Pyx_IsSubtype(Py_TYPE(obj), (PyTypeObject *)type) 1004 | static CYTHON_INLINE int __Pyx_IsSubtype(PyTypeObject *a, PyTypeObject *b); 1005 | static CYTHON_INLINE int __Pyx_PyErr_GivenExceptionMatches(PyObject *err, PyObject *type); 1006 | static CYTHON_INLINE int __Pyx_PyErr_GivenExceptionMatches2(PyObject *err, PyObject *type1, PyObject *type2); 1007 | #else 1008 | #define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type) 1009 | #define __Pyx_PyErr_GivenExceptionMatches(err, type) PyErr_GivenExceptionMatches(err, type) 1010 | #define __Pyx_PyErr_GivenExceptionMatches2(err, type1, type2) (PyErr_GivenExceptionMatches(err, type1) || PyErr_GivenExceptionMatches(err, type2)) 1011 | #endif 1012 | #define __Pyx_PyException_Check(obj) __Pyx_TypeCheck(obj, PyExc_Exception) 1013 | 1014 | /* CheckBinaryVersion.proto */ 1015 | static int __Pyx_check_binary_version(void); 1016 | 1017 | /* InitStrings.proto */ 1018 | static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); 1019 | 1020 | 1021 | /* Module declarations from 'relu' */ 1022 | #define __Pyx_MODULE_NAME "relu" 1023 | extern int __pyx_module_is_main_relu; 1024 | int __pyx_module_is_main_relu = 0; 1025 | 1026 | /* Implementation of 'relu' */ 1027 | static const char __pyx_k_n[] = "n"; 1028 | static const char __pyx_k_main[] = "__main__"; 1029 | static const char __pyx_k_name[] = "__name__"; 1030 | static const char __pyx_k_relu[] = "relu"; 1031 | static const char __pyx_k_test[] = "__test__"; 1032 | static const char __pyx_k_relu_pyx[] = "relu.pyx"; 1033 | static const char __pyx_k_cline_in_traceback[] = "cline_in_traceback"; 1034 | static PyObject *__pyx_n_s_cline_in_traceback; 1035 | static PyObject *__pyx_n_s_main; 1036 | static PyObject *__pyx_n_s_n; 1037 | static PyObject *__pyx_n_s_name; 1038 | static PyObject *__pyx_n_s_relu; 1039 | static PyObject *__pyx_kp_s_relu_pyx; 1040 | static PyObject *__pyx_n_s_test; 1041 | static PyObject *__pyx_pf_4relu_relu(CYTHON_UNUSED PyObject *__pyx_self, double __pyx_v_n); /* proto */ 1042 | static PyObject *__pyx_int_0; 1043 | static PyObject *__pyx_tuple_; 1044 | static PyObject *__pyx_codeobj__2; 1045 | /* Late includes */ 1046 | 1047 | /* "relu.pyx":1 1048 | * def relu(double n): # <<<<<<<<<<<<<< 1049 | * if n < 0: 1050 | * return 0 1051 | */ 1052 | 1053 | /* Python wrapper */ 1054 | static PyObject *__pyx_pw_4relu_1relu(PyObject *__pyx_self, PyObject *__pyx_arg_n); /*proto*/ 1055 | static PyMethodDef __pyx_mdef_4relu_1relu = {"relu", (PyCFunction)__pyx_pw_4relu_1relu, METH_O, 0}; 1056 | static PyObject *__pyx_pw_4relu_1relu(PyObject *__pyx_self, PyObject *__pyx_arg_n) { 1057 | double __pyx_v_n; 1058 | int __pyx_lineno = 0; 1059 | const char *__pyx_filename = NULL; 1060 | int __pyx_clineno = 0; 1061 | PyObject *__pyx_r = 0; 1062 | __Pyx_RefNannyDeclarations 1063 | __Pyx_RefNannySetupContext("relu (wrapper)", 0); 1064 | assert(__pyx_arg_n); { 1065 | __pyx_v_n = __pyx_PyFloat_AsDouble(__pyx_arg_n); if (unlikely((__pyx_v_n == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 1, __pyx_L3_error) 1066 | } 1067 | goto __pyx_L4_argument_unpacking_done; 1068 | __pyx_L3_error:; 1069 | __Pyx_AddTraceback("relu.relu", __pyx_clineno, __pyx_lineno, __pyx_filename); 1070 | __Pyx_RefNannyFinishContext(); 1071 | return NULL; 1072 | __pyx_L4_argument_unpacking_done:; 1073 | __pyx_r = __pyx_pf_4relu_relu(__pyx_self, ((double)__pyx_v_n)); 1074 | 1075 | /* function exit code */ 1076 | __Pyx_RefNannyFinishContext(); 1077 | return __pyx_r; 1078 | } 1079 | 1080 | static PyObject *__pyx_pf_4relu_relu(CYTHON_UNUSED PyObject *__pyx_self, double __pyx_v_n) { 1081 | PyObject *__pyx_r = NULL; 1082 | __Pyx_RefNannyDeclarations 1083 | int __pyx_t_1; 1084 | PyObject *__pyx_t_2 = NULL; 1085 | int __pyx_lineno = 0; 1086 | const char *__pyx_filename = NULL; 1087 | int __pyx_clineno = 0; 1088 | __Pyx_RefNannySetupContext("relu", 0); 1089 | 1090 | /* "relu.pyx":2 1091 | * def relu(double n): 1092 | * if n < 0: # <<<<<<<<<<<<<< 1093 | * return 0 1094 | * return n 1095 | */ 1096 | __pyx_t_1 = ((__pyx_v_n < 0.0) != 0); 1097 | if (__pyx_t_1) { 1098 | 1099 | /* "relu.pyx":3 1100 | * def relu(double n): 1101 | * if n < 0: 1102 | * return 0 # <<<<<<<<<<<<<< 1103 | * return n 1104 | */ 1105 | __Pyx_XDECREF(__pyx_r); 1106 | __Pyx_INCREF(__pyx_int_0); 1107 | __pyx_r = __pyx_int_0; 1108 | goto __pyx_L0; 1109 | 1110 | /* "relu.pyx":2 1111 | * def relu(double n): 1112 | * if n < 0: # <<<<<<<<<<<<<< 1113 | * return 0 1114 | * return n 1115 | */ 1116 | } 1117 | 1118 | /* "relu.pyx":4 1119 | * if n < 0: 1120 | * return 0 1121 | * return n # <<<<<<<<<<<<<< 1122 | */ 1123 | __Pyx_XDECREF(__pyx_r); 1124 | __pyx_t_2 = PyFloat_FromDouble(__pyx_v_n); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 4, __pyx_L1_error) 1125 | __Pyx_GOTREF(__pyx_t_2); 1126 | __pyx_r = __pyx_t_2; 1127 | __pyx_t_2 = 0; 1128 | goto __pyx_L0; 1129 | 1130 | /* "relu.pyx":1 1131 | * def relu(double n): # <<<<<<<<<<<<<< 1132 | * if n < 0: 1133 | * return 0 1134 | */ 1135 | 1136 | /* function exit code */ 1137 | __pyx_L1_error:; 1138 | __Pyx_XDECREF(__pyx_t_2); 1139 | __Pyx_AddTraceback("relu.relu", __pyx_clineno, __pyx_lineno, __pyx_filename); 1140 | __pyx_r = NULL; 1141 | __pyx_L0:; 1142 | __Pyx_XGIVEREF(__pyx_r); 1143 | __Pyx_RefNannyFinishContext(); 1144 | return __pyx_r; 1145 | } 1146 | 1147 | static PyMethodDef __pyx_methods[] = { 1148 | {0, 0, 0, 0} 1149 | }; 1150 | 1151 | #if PY_MAJOR_VERSION >= 3 1152 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1153 | static PyObject* __pyx_pymod_create(PyObject *spec, PyModuleDef *def); /*proto*/ 1154 | static int __pyx_pymod_exec_relu(PyObject* module); /*proto*/ 1155 | static PyModuleDef_Slot __pyx_moduledef_slots[] = { 1156 | {Py_mod_create, (void*)__pyx_pymod_create}, 1157 | {Py_mod_exec, (void*)__pyx_pymod_exec_relu}, 1158 | {0, NULL} 1159 | }; 1160 | #endif 1161 | 1162 | static struct PyModuleDef __pyx_moduledef = { 1163 | PyModuleDef_HEAD_INIT, 1164 | "relu", 1165 | 0, /* m_doc */ 1166 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1167 | 0, /* m_size */ 1168 | #else 1169 | -1, /* m_size */ 1170 | #endif 1171 | __pyx_methods /* m_methods */, 1172 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1173 | __pyx_moduledef_slots, /* m_slots */ 1174 | #else 1175 | NULL, /* m_reload */ 1176 | #endif 1177 | NULL, /* m_traverse */ 1178 | NULL, /* m_clear */ 1179 | NULL /* m_free */ 1180 | }; 1181 | #endif 1182 | #ifndef CYTHON_SMALL_CODE 1183 | #if defined(__clang__) 1184 | #define CYTHON_SMALL_CODE 1185 | #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 1186 | #define CYTHON_SMALL_CODE __attribute__((cold)) 1187 | #else 1188 | #define CYTHON_SMALL_CODE 1189 | #endif 1190 | #endif 1191 | 1192 | static __Pyx_StringTabEntry __pyx_string_tab[] = { 1193 | {&__pyx_n_s_cline_in_traceback, __pyx_k_cline_in_traceback, sizeof(__pyx_k_cline_in_traceback), 0, 0, 1, 1}, 1194 | {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1}, 1195 | {&__pyx_n_s_n, __pyx_k_n, sizeof(__pyx_k_n), 0, 0, 1, 1}, 1196 | {&__pyx_n_s_name, __pyx_k_name, sizeof(__pyx_k_name), 0, 0, 1, 1}, 1197 | {&__pyx_n_s_relu, __pyx_k_relu, sizeof(__pyx_k_relu), 0, 0, 1, 1}, 1198 | {&__pyx_kp_s_relu_pyx, __pyx_k_relu_pyx, sizeof(__pyx_k_relu_pyx), 0, 0, 1, 0}, 1199 | {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1}, 1200 | {0, 0, 0, 0, 0, 0, 0} 1201 | }; 1202 | static CYTHON_SMALL_CODE int __Pyx_InitCachedBuiltins(void) { 1203 | return 0; 1204 | } 1205 | 1206 | static CYTHON_SMALL_CODE int __Pyx_InitCachedConstants(void) { 1207 | __Pyx_RefNannyDeclarations 1208 | __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0); 1209 | 1210 | /* "relu.pyx":1 1211 | * def relu(double n): # <<<<<<<<<<<<<< 1212 | * if n < 0: 1213 | * return 0 1214 | */ 1215 | __pyx_tuple_ = PyTuple_Pack(2, __pyx_n_s_n, __pyx_n_s_n); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 1, __pyx_L1_error) 1216 | __Pyx_GOTREF(__pyx_tuple_); 1217 | __Pyx_GIVEREF(__pyx_tuple_); 1218 | __pyx_codeobj__2 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple_, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_relu_pyx, __pyx_n_s_relu, 1, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__2)) __PYX_ERR(0, 1, __pyx_L1_error) 1219 | __Pyx_RefNannyFinishContext(); 1220 | return 0; 1221 | __pyx_L1_error:; 1222 | __Pyx_RefNannyFinishContext(); 1223 | return -1; 1224 | } 1225 | 1226 | static CYTHON_SMALL_CODE int __Pyx_InitGlobals(void) { 1227 | if (__Pyx_InitStrings(__pyx_string_tab) < 0) __PYX_ERR(0, 1, __pyx_L1_error); 1228 | __pyx_int_0 = PyInt_FromLong(0); if (unlikely(!__pyx_int_0)) __PYX_ERR(0, 1, __pyx_L1_error) 1229 | return 0; 1230 | __pyx_L1_error:; 1231 | return -1; 1232 | } 1233 | 1234 | static CYTHON_SMALL_CODE int __Pyx_modinit_global_init_code(void); /*proto*/ 1235 | static CYTHON_SMALL_CODE int __Pyx_modinit_variable_export_code(void); /*proto*/ 1236 | static CYTHON_SMALL_CODE int __Pyx_modinit_function_export_code(void); /*proto*/ 1237 | static CYTHON_SMALL_CODE int __Pyx_modinit_type_init_code(void); /*proto*/ 1238 | static CYTHON_SMALL_CODE int __Pyx_modinit_type_import_code(void); /*proto*/ 1239 | static CYTHON_SMALL_CODE int __Pyx_modinit_variable_import_code(void); /*proto*/ 1240 | static CYTHON_SMALL_CODE int __Pyx_modinit_function_import_code(void); /*proto*/ 1241 | 1242 | static int __Pyx_modinit_global_init_code(void) { 1243 | __Pyx_RefNannyDeclarations 1244 | __Pyx_RefNannySetupContext("__Pyx_modinit_global_init_code", 0); 1245 | /*--- Global init code ---*/ 1246 | __Pyx_RefNannyFinishContext(); 1247 | return 0; 1248 | } 1249 | 1250 | static int __Pyx_modinit_variable_export_code(void) { 1251 | __Pyx_RefNannyDeclarations 1252 | __Pyx_RefNannySetupContext("__Pyx_modinit_variable_export_code", 0); 1253 | /*--- Variable export code ---*/ 1254 | __Pyx_RefNannyFinishContext(); 1255 | return 0; 1256 | } 1257 | 1258 | static int __Pyx_modinit_function_export_code(void) { 1259 | __Pyx_RefNannyDeclarations 1260 | __Pyx_RefNannySetupContext("__Pyx_modinit_function_export_code", 0); 1261 | /*--- Function export code ---*/ 1262 | __Pyx_RefNannyFinishContext(); 1263 | return 0; 1264 | } 1265 | 1266 | static int __Pyx_modinit_type_init_code(void) { 1267 | __Pyx_RefNannyDeclarations 1268 | __Pyx_RefNannySetupContext("__Pyx_modinit_type_init_code", 0); 1269 | /*--- Type init code ---*/ 1270 | __Pyx_RefNannyFinishContext(); 1271 | return 0; 1272 | } 1273 | 1274 | static int __Pyx_modinit_type_import_code(void) { 1275 | __Pyx_RefNannyDeclarations 1276 | __Pyx_RefNannySetupContext("__Pyx_modinit_type_import_code", 0); 1277 | /*--- Type import code ---*/ 1278 | __Pyx_RefNannyFinishContext(); 1279 | return 0; 1280 | } 1281 | 1282 | static int __Pyx_modinit_variable_import_code(void) { 1283 | __Pyx_RefNannyDeclarations 1284 | __Pyx_RefNannySetupContext("__Pyx_modinit_variable_import_code", 0); 1285 | /*--- Variable import code ---*/ 1286 | __Pyx_RefNannyFinishContext(); 1287 | return 0; 1288 | } 1289 | 1290 | static int __Pyx_modinit_function_import_code(void) { 1291 | __Pyx_RefNannyDeclarations 1292 | __Pyx_RefNannySetupContext("__Pyx_modinit_function_import_code", 0); 1293 | /*--- Function import code ---*/ 1294 | __Pyx_RefNannyFinishContext(); 1295 | return 0; 1296 | } 1297 | 1298 | 1299 | #ifndef CYTHON_NO_PYINIT_EXPORT 1300 | #define __Pyx_PyMODINIT_FUNC PyMODINIT_FUNC 1301 | #elif PY_MAJOR_VERSION < 3 1302 | #ifdef __cplusplus 1303 | #define __Pyx_PyMODINIT_FUNC extern "C" void 1304 | #else 1305 | #define __Pyx_PyMODINIT_FUNC void 1306 | #endif 1307 | #else 1308 | #ifdef __cplusplus 1309 | #define __Pyx_PyMODINIT_FUNC extern "C" PyObject * 1310 | #else 1311 | #define __Pyx_PyMODINIT_FUNC PyObject * 1312 | #endif 1313 | #endif 1314 | 1315 | 1316 | #if PY_MAJOR_VERSION < 3 1317 | __Pyx_PyMODINIT_FUNC initrelu(void) CYTHON_SMALL_CODE; /*proto*/ 1318 | __Pyx_PyMODINIT_FUNC initrelu(void) 1319 | #else 1320 | __Pyx_PyMODINIT_FUNC PyInit_relu(void) CYTHON_SMALL_CODE; /*proto*/ 1321 | __Pyx_PyMODINIT_FUNC PyInit_relu(void) 1322 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1323 | { 1324 | return PyModuleDef_Init(&__pyx_moduledef); 1325 | } 1326 | static CYTHON_SMALL_CODE int __Pyx_check_single_interpreter(void) { 1327 | #if PY_VERSION_HEX >= 0x030700A1 1328 | static PY_INT64_T main_interpreter_id = -1; 1329 | PY_INT64_T current_id = PyInterpreterState_GetID(PyThreadState_Get()->interp); 1330 | if (main_interpreter_id == -1) { 1331 | main_interpreter_id = current_id; 1332 | return (unlikely(current_id == -1)) ? -1 : 0; 1333 | } else if (unlikely(main_interpreter_id != current_id)) 1334 | #else 1335 | static PyInterpreterState *main_interpreter = NULL; 1336 | PyInterpreterState *current_interpreter = PyThreadState_Get()->interp; 1337 | if (!main_interpreter) { 1338 | main_interpreter = current_interpreter; 1339 | } else if (unlikely(main_interpreter != current_interpreter)) 1340 | #endif 1341 | { 1342 | PyErr_SetString( 1343 | PyExc_ImportError, 1344 | "Interpreter change detected - this module can only be loaded into one interpreter per process."); 1345 | return -1; 1346 | } 1347 | return 0; 1348 | } 1349 | static CYTHON_SMALL_CODE int __Pyx_copy_spec_to_module(PyObject *spec, PyObject *moddict, const char* from_name, const char* to_name, int allow_none) { 1350 | PyObject *value = PyObject_GetAttrString(spec, from_name); 1351 | int result = 0; 1352 | if (likely(value)) { 1353 | if (allow_none || value != Py_None) { 1354 | result = PyDict_SetItemString(moddict, to_name, value); 1355 | } 1356 | Py_DECREF(value); 1357 | } else if (PyErr_ExceptionMatches(PyExc_AttributeError)) { 1358 | PyErr_Clear(); 1359 | } else { 1360 | result = -1; 1361 | } 1362 | return result; 1363 | } 1364 | static CYTHON_SMALL_CODE PyObject* __pyx_pymod_create(PyObject *spec, CYTHON_UNUSED PyModuleDef *def) { 1365 | PyObject *module = NULL, *moddict, *modname; 1366 | if (__Pyx_check_single_interpreter()) 1367 | return NULL; 1368 | if (__pyx_m) 1369 | return __Pyx_NewRef(__pyx_m); 1370 | modname = PyObject_GetAttrString(spec, "name"); 1371 | if (unlikely(!modname)) goto bad; 1372 | module = PyModule_NewObject(modname); 1373 | Py_DECREF(modname); 1374 | if (unlikely(!module)) goto bad; 1375 | moddict = PyModule_GetDict(module); 1376 | if (unlikely(!moddict)) goto bad; 1377 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "loader", "__loader__", 1) < 0)) goto bad; 1378 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "origin", "__file__", 1) < 0)) goto bad; 1379 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "parent", "__package__", 1) < 0)) goto bad; 1380 | if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "submodule_search_locations", "__path__", 0) < 0)) goto bad; 1381 | return module; 1382 | bad: 1383 | Py_XDECREF(module); 1384 | return NULL; 1385 | } 1386 | 1387 | 1388 | static CYTHON_SMALL_CODE int __pyx_pymod_exec_relu(PyObject *__pyx_pyinit_module) 1389 | #endif 1390 | #endif 1391 | { 1392 | PyObject *__pyx_t_1 = NULL; 1393 | int __pyx_lineno = 0; 1394 | const char *__pyx_filename = NULL; 1395 | int __pyx_clineno = 0; 1396 | __Pyx_RefNannyDeclarations 1397 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1398 | if (__pyx_m) { 1399 | if (__pyx_m == __pyx_pyinit_module) return 0; 1400 | PyErr_SetString(PyExc_RuntimeError, "Module 'relu' has already been imported. Re-initialisation is not supported."); 1401 | return -1; 1402 | } 1403 | #elif PY_MAJOR_VERSION >= 3 1404 | if (__pyx_m) return __Pyx_NewRef(__pyx_m); 1405 | #endif 1406 | #if CYTHON_REFNANNY 1407 | __Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny"); 1408 | if (!__Pyx_RefNanny) { 1409 | PyErr_Clear(); 1410 | __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny"); 1411 | if (!__Pyx_RefNanny) 1412 | Py_FatalError("failed to import 'refnanny' module"); 1413 | } 1414 | #endif 1415 | __Pyx_RefNannySetupContext("__Pyx_PyMODINIT_FUNC PyInit_relu(void)", 0); 1416 | if (__Pyx_check_binary_version() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1417 | #ifdef __Pxy_PyFrame_Initialize_Offsets 1418 | __Pxy_PyFrame_Initialize_Offsets(); 1419 | #endif 1420 | __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) __PYX_ERR(0, 1, __pyx_L1_error) 1421 | __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) __PYX_ERR(0, 1, __pyx_L1_error) 1422 | __pyx_empty_unicode = PyUnicode_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_unicode)) __PYX_ERR(0, 1, __pyx_L1_error) 1423 | #ifdef __Pyx_CyFunction_USED 1424 | if (__pyx_CyFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1425 | #endif 1426 | #ifdef __Pyx_FusedFunction_USED 1427 | if (__pyx_FusedFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1428 | #endif 1429 | #ifdef __Pyx_Coroutine_USED 1430 | if (__pyx_Coroutine_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1431 | #endif 1432 | #ifdef __Pyx_Generator_USED 1433 | if (__pyx_Generator_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1434 | #endif 1435 | #ifdef __Pyx_AsyncGen_USED 1436 | if (__pyx_AsyncGen_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1437 | #endif 1438 | #ifdef __Pyx_StopAsyncIteration_USED 1439 | if (__pyx_StopAsyncIteration_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1440 | #endif 1441 | /*--- Library function declarations ---*/ 1442 | /*--- Threads initialization code ---*/ 1443 | #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS 1444 | #ifdef WITH_THREAD /* Python build with threading support? */ 1445 | PyEval_InitThreads(); 1446 | #endif 1447 | #endif 1448 | /*--- Module creation code ---*/ 1449 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1450 | __pyx_m = __pyx_pyinit_module; 1451 | Py_INCREF(__pyx_m); 1452 | #else 1453 | #if PY_MAJOR_VERSION < 3 1454 | __pyx_m = Py_InitModule4("relu", __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m); 1455 | #else 1456 | __pyx_m = PyModule_Create(&__pyx_moduledef); 1457 | #endif 1458 | if (unlikely(!__pyx_m)) __PYX_ERR(0, 1, __pyx_L1_error) 1459 | #endif 1460 | __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) __PYX_ERR(0, 1, __pyx_L1_error) 1461 | Py_INCREF(__pyx_d); 1462 | __pyx_b = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME); if (unlikely(!__pyx_b)) __PYX_ERR(0, 1, __pyx_L1_error) 1463 | Py_INCREF(__pyx_b); 1464 | __pyx_cython_runtime = PyImport_AddModule((char *) "cython_runtime"); if (unlikely(!__pyx_cython_runtime)) __PYX_ERR(0, 1, __pyx_L1_error) 1465 | Py_INCREF(__pyx_cython_runtime); 1466 | if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) __PYX_ERR(0, 1, __pyx_L1_error); 1467 | /*--- Initialize various global constants etc. ---*/ 1468 | if (__Pyx_InitGlobals() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1469 | #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT) 1470 | if (__Pyx_init_sys_getdefaultencoding_params() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1471 | #endif 1472 | if (__pyx_module_is_main_relu) { 1473 | if (PyObject_SetAttr(__pyx_m, __pyx_n_s_name, __pyx_n_s_main) < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1474 | } 1475 | #if PY_MAJOR_VERSION >= 3 1476 | { 1477 | PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) __PYX_ERR(0, 1, __pyx_L1_error) 1478 | if (!PyDict_GetItemString(modules, "relu")) { 1479 | if (unlikely(PyDict_SetItemString(modules, "relu", __pyx_m) < 0)) __PYX_ERR(0, 1, __pyx_L1_error) 1480 | } 1481 | } 1482 | #endif 1483 | /*--- Builtin init code ---*/ 1484 | if (__Pyx_InitCachedBuiltins() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1485 | /*--- Constants init code ---*/ 1486 | if (__Pyx_InitCachedConstants() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1487 | /*--- Global type/function init code ---*/ 1488 | (void)__Pyx_modinit_global_init_code(); 1489 | (void)__Pyx_modinit_variable_export_code(); 1490 | (void)__Pyx_modinit_function_export_code(); 1491 | (void)__Pyx_modinit_type_init_code(); 1492 | (void)__Pyx_modinit_type_import_code(); 1493 | (void)__Pyx_modinit_variable_import_code(); 1494 | (void)__Pyx_modinit_function_import_code(); 1495 | /*--- Execution code ---*/ 1496 | #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED) 1497 | if (__Pyx_patch_abc() < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1498 | #endif 1499 | 1500 | /* "relu.pyx":1 1501 | * def relu(double n): # <<<<<<<<<<<<<< 1502 | * if n < 0: 1503 | * return 0 1504 | */ 1505 | __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_4relu_1relu, NULL, __pyx_n_s_relu); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error) 1506 | __Pyx_GOTREF(__pyx_t_1); 1507 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_relu, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1508 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1509 | __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error) 1510 | __Pyx_GOTREF(__pyx_t_1); 1511 | if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error) 1512 | __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; 1513 | 1514 | /*--- Wrapped vars code ---*/ 1515 | 1516 | goto __pyx_L0; 1517 | __pyx_L1_error:; 1518 | __Pyx_XDECREF(__pyx_t_1); 1519 | if (__pyx_m) { 1520 | if (__pyx_d) { 1521 | __Pyx_AddTraceback("init relu", __pyx_clineno, __pyx_lineno, __pyx_filename); 1522 | } 1523 | Py_CLEAR(__pyx_m); 1524 | } else if (!PyErr_Occurred()) { 1525 | PyErr_SetString(PyExc_ImportError, "init relu"); 1526 | } 1527 | __pyx_L0:; 1528 | __Pyx_RefNannyFinishContext(); 1529 | #if CYTHON_PEP489_MULTI_PHASE_INIT 1530 | return (__pyx_m != NULL) ? 0 : -1; 1531 | #elif PY_MAJOR_VERSION >= 3 1532 | return __pyx_m; 1533 | #else 1534 | return; 1535 | #endif 1536 | } 1537 | 1538 | /* --- Runtime support code --- */ 1539 | /* Refnanny */ 1540 | #if CYTHON_REFNANNY 1541 | static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) { 1542 | PyObject *m = NULL, *p = NULL; 1543 | void *r = NULL; 1544 | m = PyImport_ImportModule(modname); 1545 | if (!m) goto end; 1546 | p = PyObject_GetAttrString(m, "RefNannyAPI"); 1547 | if (!p) goto end; 1548 | r = PyLong_AsVoidPtr(p); 1549 | end: 1550 | Py_XDECREF(p); 1551 | Py_XDECREF(m); 1552 | return (__Pyx_RefNannyAPIStruct *)r; 1553 | } 1554 | #endif 1555 | 1556 | /* PyDictVersioning */ 1557 | #if CYTHON_USE_DICT_VERSIONS && CYTHON_USE_TYPE_SLOTS 1558 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_tp_dict_version(PyObject *obj) { 1559 | PyObject *dict = Py_TYPE(obj)->tp_dict; 1560 | return likely(dict) ? __PYX_GET_DICT_VERSION(dict) : 0; 1561 | } 1562 | static CYTHON_INLINE PY_UINT64_T __Pyx_get_object_dict_version(PyObject *obj) { 1563 | PyObject **dictptr = NULL; 1564 | Py_ssize_t offset = Py_TYPE(obj)->tp_dictoffset; 1565 | if (offset) { 1566 | #if CYTHON_COMPILING_IN_CPYTHON 1567 | dictptr = (likely(offset > 0)) ? (PyObject **) ((char *)obj + offset) : _PyObject_GetDictPtr(obj); 1568 | #else 1569 | dictptr = _PyObject_GetDictPtr(obj); 1570 | #endif 1571 | } 1572 | return (dictptr && *dictptr) ? __PYX_GET_DICT_VERSION(*dictptr) : 0; 1573 | } 1574 | static CYTHON_INLINE int __Pyx_object_dict_version_matches(PyObject* obj, PY_UINT64_T tp_dict_version, PY_UINT64_T obj_dict_version) { 1575 | PyObject *dict = Py_TYPE(obj)->tp_dict; 1576 | if (unlikely(!dict) || unlikely(tp_dict_version != __PYX_GET_DICT_VERSION(dict))) 1577 | return 0; 1578 | return obj_dict_version == __Pyx_get_object_dict_version(obj); 1579 | } 1580 | #endif 1581 | 1582 | /* PyObjectGetAttrStr */ 1583 | #if CYTHON_USE_TYPE_SLOTS 1584 | static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) { 1585 | PyTypeObject* tp = Py_TYPE(obj); 1586 | if (likely(tp->tp_getattro)) 1587 | return tp->tp_getattro(obj, attr_name); 1588 | #if PY_MAJOR_VERSION < 3 1589 | if (likely(tp->tp_getattr)) 1590 | return tp->tp_getattr(obj, PyString_AS_STRING(attr_name)); 1591 | #endif 1592 | return PyObject_GetAttr(obj, attr_name); 1593 | } 1594 | #endif 1595 | 1596 | /* PyErrFetchRestore */ 1597 | #if CYTHON_FAST_THREAD_STATE 1598 | static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) { 1599 | PyObject *tmp_type, *tmp_value, *tmp_tb; 1600 | tmp_type = tstate->curexc_type; 1601 | tmp_value = tstate->curexc_value; 1602 | tmp_tb = tstate->curexc_traceback; 1603 | tstate->curexc_type = type; 1604 | tstate->curexc_value = value; 1605 | tstate->curexc_traceback = tb; 1606 | Py_XDECREF(tmp_type); 1607 | Py_XDECREF(tmp_value); 1608 | Py_XDECREF(tmp_tb); 1609 | } 1610 | static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) { 1611 | *type = tstate->curexc_type; 1612 | *value = tstate->curexc_value; 1613 | *tb = tstate->curexc_traceback; 1614 | tstate->curexc_type = 0; 1615 | tstate->curexc_value = 0; 1616 | tstate->curexc_traceback = 0; 1617 | } 1618 | #endif 1619 | 1620 | /* CLineInTraceback */ 1621 | #ifndef CYTHON_CLINE_IN_TRACEBACK 1622 | static int __Pyx_CLineForTraceback(CYTHON_NCP_UNUSED PyThreadState *tstate, int c_line) { 1623 | PyObject *use_cline; 1624 | PyObject *ptype, *pvalue, *ptraceback; 1625 | #if CYTHON_COMPILING_IN_CPYTHON 1626 | PyObject **cython_runtime_dict; 1627 | #endif 1628 | if (unlikely(!__pyx_cython_runtime)) { 1629 | return c_line; 1630 | } 1631 | __Pyx_ErrFetchInState(tstate, &ptype, &pvalue, &ptraceback); 1632 | #if CYTHON_COMPILING_IN_CPYTHON 1633 | cython_runtime_dict = _PyObject_GetDictPtr(__pyx_cython_runtime); 1634 | if (likely(cython_runtime_dict)) { 1635 | __PYX_PY_DICT_LOOKUP_IF_MODIFIED( 1636 | use_cline, *cython_runtime_dict, 1637 | __Pyx_PyDict_GetItemStr(*cython_runtime_dict, __pyx_n_s_cline_in_traceback)) 1638 | } else 1639 | #endif 1640 | { 1641 | PyObject *use_cline_obj = __Pyx_PyObject_GetAttrStr(__pyx_cython_runtime, __pyx_n_s_cline_in_traceback); 1642 | if (use_cline_obj) { 1643 | use_cline = PyObject_Not(use_cline_obj) ? Py_False : Py_True; 1644 | Py_DECREF(use_cline_obj); 1645 | } else { 1646 | PyErr_Clear(); 1647 | use_cline = NULL; 1648 | } 1649 | } 1650 | if (!use_cline) { 1651 | c_line = 0; 1652 | PyObject_SetAttr(__pyx_cython_runtime, __pyx_n_s_cline_in_traceback, Py_False); 1653 | } 1654 | else if (use_cline == Py_False || (use_cline != Py_True && PyObject_Not(use_cline) != 0)) { 1655 | c_line = 0; 1656 | } 1657 | __Pyx_ErrRestoreInState(tstate, ptype, pvalue, ptraceback); 1658 | return c_line; 1659 | } 1660 | #endif 1661 | 1662 | /* CodeObjectCache */ 1663 | static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) { 1664 | int start = 0, mid = 0, end = count - 1; 1665 | if (end >= 0 && code_line > entries[end].code_line) { 1666 | return count; 1667 | } 1668 | while (start < end) { 1669 | mid = start + (end - start) / 2; 1670 | if (code_line < entries[mid].code_line) { 1671 | end = mid; 1672 | } else if (code_line > entries[mid].code_line) { 1673 | start = mid + 1; 1674 | } else { 1675 | return mid; 1676 | } 1677 | } 1678 | if (code_line <= entries[mid].code_line) { 1679 | return mid; 1680 | } else { 1681 | return mid + 1; 1682 | } 1683 | } 1684 | static PyCodeObject *__pyx_find_code_object(int code_line) { 1685 | PyCodeObject* code_object; 1686 | int pos; 1687 | if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) { 1688 | return NULL; 1689 | } 1690 | pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); 1691 | if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) { 1692 | return NULL; 1693 | } 1694 | code_object = __pyx_code_cache.entries[pos].code_object; 1695 | Py_INCREF(code_object); 1696 | return code_object; 1697 | } 1698 | static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) { 1699 | int pos, i; 1700 | __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; 1701 | if (unlikely(!code_line)) { 1702 | return; 1703 | } 1704 | if (unlikely(!entries)) { 1705 | entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry)); 1706 | if (likely(entries)) { 1707 | __pyx_code_cache.entries = entries; 1708 | __pyx_code_cache.max_count = 64; 1709 | __pyx_code_cache.count = 1; 1710 | entries[0].code_line = code_line; 1711 | entries[0].code_object = code_object; 1712 | Py_INCREF(code_object); 1713 | } 1714 | return; 1715 | } 1716 | pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); 1717 | if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) { 1718 | PyCodeObject* tmp = entries[pos].code_object; 1719 | entries[pos].code_object = code_object; 1720 | Py_DECREF(tmp); 1721 | return; 1722 | } 1723 | if (__pyx_code_cache.count == __pyx_code_cache.max_count) { 1724 | int new_max = __pyx_code_cache.max_count + 64; 1725 | entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc( 1726 | __pyx_code_cache.entries, ((size_t)new_max) * sizeof(__Pyx_CodeObjectCacheEntry)); 1727 | if (unlikely(!entries)) { 1728 | return; 1729 | } 1730 | __pyx_code_cache.entries = entries; 1731 | __pyx_code_cache.max_count = new_max; 1732 | } 1733 | for (i=__pyx_code_cache.count; i>pos; i--) { 1734 | entries[i] = entries[i-1]; 1735 | } 1736 | entries[pos].code_line = code_line; 1737 | entries[pos].code_object = code_object; 1738 | __pyx_code_cache.count++; 1739 | Py_INCREF(code_object); 1740 | } 1741 | 1742 | /* AddTraceback */ 1743 | #include "compile.h" 1744 | #include "frameobject.h" 1745 | #include "traceback.h" 1746 | static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( 1747 | const char *funcname, int c_line, 1748 | int py_line, const char *filename) { 1749 | PyCodeObject *py_code = 0; 1750 | PyObject *py_srcfile = 0; 1751 | PyObject *py_funcname = 0; 1752 | #if PY_MAJOR_VERSION < 3 1753 | py_srcfile = PyString_FromString(filename); 1754 | #else 1755 | py_srcfile = PyUnicode_FromString(filename); 1756 | #endif 1757 | if (!py_srcfile) goto bad; 1758 | if (c_line) { 1759 | #if PY_MAJOR_VERSION < 3 1760 | py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); 1761 | #else 1762 | py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); 1763 | #endif 1764 | } 1765 | else { 1766 | #if PY_MAJOR_VERSION < 3 1767 | py_funcname = PyString_FromString(funcname); 1768 | #else 1769 | py_funcname = PyUnicode_FromString(funcname); 1770 | #endif 1771 | } 1772 | if (!py_funcname) goto bad; 1773 | py_code = __Pyx_PyCode_New( 1774 | 0, 1775 | 0, 1776 | 0, 1777 | 0, 1778 | 0, 1779 | __pyx_empty_bytes, /*PyObject *code,*/ 1780 | __pyx_empty_tuple, /*PyObject *consts,*/ 1781 | __pyx_empty_tuple, /*PyObject *names,*/ 1782 | __pyx_empty_tuple, /*PyObject *varnames,*/ 1783 | __pyx_empty_tuple, /*PyObject *freevars,*/ 1784 | __pyx_empty_tuple, /*PyObject *cellvars,*/ 1785 | py_srcfile, /*PyObject *filename,*/ 1786 | py_funcname, /*PyObject *name,*/ 1787 | py_line, 1788 | __pyx_empty_bytes /*PyObject *lnotab*/ 1789 | ); 1790 | Py_DECREF(py_srcfile); 1791 | Py_DECREF(py_funcname); 1792 | return py_code; 1793 | bad: 1794 | Py_XDECREF(py_srcfile); 1795 | Py_XDECREF(py_funcname); 1796 | return NULL; 1797 | } 1798 | static void __Pyx_AddTraceback(const char *funcname, int c_line, 1799 | int py_line, const char *filename) { 1800 | PyCodeObject *py_code = 0; 1801 | PyFrameObject *py_frame = 0; 1802 | PyThreadState *tstate = __Pyx_PyThreadState_Current; 1803 | if (c_line) { 1804 | c_line = __Pyx_CLineForTraceback(tstate, c_line); 1805 | } 1806 | py_code = __pyx_find_code_object(c_line ? -c_line : py_line); 1807 | if (!py_code) { 1808 | py_code = __Pyx_CreateCodeObjectForTraceback( 1809 | funcname, c_line, py_line, filename); 1810 | if (!py_code) goto bad; 1811 | __pyx_insert_code_object(c_line ? -c_line : py_line, py_code); 1812 | } 1813 | py_frame = PyFrame_New( 1814 | tstate, /*PyThreadState *tstate,*/ 1815 | py_code, /*PyCodeObject *code,*/ 1816 | __pyx_d, /*PyObject *globals,*/ 1817 | 0 /*PyObject *locals*/ 1818 | ); 1819 | if (!py_frame) goto bad; 1820 | __Pyx_PyFrame_SetLineNumber(py_frame, py_line); 1821 | PyTraceBack_Here(py_frame); 1822 | bad: 1823 | Py_XDECREF(py_code); 1824 | Py_XDECREF(py_frame); 1825 | } 1826 | 1827 | /* CIntToPy */ 1828 | static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) { 1829 | const long neg_one = (long) ((long) 0 - (long) 1), const_zero = (long) 0; 1830 | const int is_unsigned = neg_one > const_zero; 1831 | if (is_unsigned) { 1832 | if (sizeof(long) < sizeof(long)) { 1833 | return PyInt_FromLong((long) value); 1834 | } else if (sizeof(long) <= sizeof(unsigned long)) { 1835 | return PyLong_FromUnsignedLong((unsigned long) value); 1836 | #ifdef HAVE_LONG_LONG 1837 | } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { 1838 | return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value); 1839 | #endif 1840 | } 1841 | } else { 1842 | if (sizeof(long) <= sizeof(long)) { 1843 | return PyInt_FromLong((long) value); 1844 | #ifdef HAVE_LONG_LONG 1845 | } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { 1846 | return PyLong_FromLongLong((PY_LONG_LONG) value); 1847 | #endif 1848 | } 1849 | } 1850 | { 1851 | int one = 1; int little = (int)*(unsigned char *)&one; 1852 | unsigned char *bytes = (unsigned char *)&value; 1853 | return _PyLong_FromByteArray(bytes, sizeof(long), 1854 | little, !is_unsigned); 1855 | } 1856 | } 1857 | 1858 | /* CIntFromPyVerify */ 1859 | #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value)\ 1860 | __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 0) 1861 | #define __PYX_VERIFY_RETURN_INT_EXC(target_type, func_type, func_value)\ 1862 | __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 1) 1863 | #define __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, exc)\ 1864 | {\ 1865 | func_type value = func_value;\ 1866 | if (sizeof(target_type) < sizeof(func_type)) {\ 1867 | if (unlikely(value != (func_type) (target_type) value)) {\ 1868 | func_type zero = 0;\ 1869 | if (exc && unlikely(value == (func_type)-1 && PyErr_Occurred()))\ 1870 | return (target_type) -1;\ 1871 | if (is_unsigned && unlikely(value < zero))\ 1872 | goto raise_neg_overflow;\ 1873 | else\ 1874 | goto raise_overflow;\ 1875 | }\ 1876 | }\ 1877 | return (target_type) value;\ 1878 | } 1879 | 1880 | /* CIntFromPy */ 1881 | static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) { 1882 | const long neg_one = (long) ((long) 0 - (long) 1), const_zero = (long) 0; 1883 | const int is_unsigned = neg_one > const_zero; 1884 | #if PY_MAJOR_VERSION < 3 1885 | if (likely(PyInt_Check(x))) { 1886 | if (sizeof(long) < sizeof(long)) { 1887 | __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG(x)) 1888 | } else { 1889 | long val = PyInt_AS_LONG(x); 1890 | if (is_unsigned && unlikely(val < 0)) { 1891 | goto raise_neg_overflow; 1892 | } 1893 | return (long) val; 1894 | } 1895 | } else 1896 | #endif 1897 | if (likely(PyLong_Check(x))) { 1898 | if (is_unsigned) { 1899 | #if CYTHON_USE_PYLONG_INTERNALS 1900 | const digit* digits = ((PyLongObject*)x)->ob_digit; 1901 | switch (Py_SIZE(x)) { 1902 | case 0: return (long) 0; 1903 | case 1: __PYX_VERIFY_RETURN_INT(long, digit, digits[0]) 1904 | case 2: 1905 | if (8 * sizeof(long) > 1 * PyLong_SHIFT) { 1906 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 1907 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1908 | } else if (8 * sizeof(long) >= 2 * PyLong_SHIFT) { 1909 | return (long) (((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); 1910 | } 1911 | } 1912 | break; 1913 | case 3: 1914 | if (8 * sizeof(long) > 2 * PyLong_SHIFT) { 1915 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 1916 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1917 | } else if (8 * sizeof(long) >= 3 * PyLong_SHIFT) { 1918 | return (long) (((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); 1919 | } 1920 | } 1921 | break; 1922 | case 4: 1923 | if (8 * sizeof(long) > 3 * PyLong_SHIFT) { 1924 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 1925 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1926 | } else if (8 * sizeof(long) >= 4 * PyLong_SHIFT) { 1927 | return (long) (((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); 1928 | } 1929 | } 1930 | break; 1931 | } 1932 | #endif 1933 | #if CYTHON_COMPILING_IN_CPYTHON 1934 | if (unlikely(Py_SIZE(x) < 0)) { 1935 | goto raise_neg_overflow; 1936 | } 1937 | #else 1938 | { 1939 | int result = PyObject_RichCompareBool(x, Py_False, Py_LT); 1940 | if (unlikely(result < 0)) 1941 | return (long) -1; 1942 | if (unlikely(result == 1)) 1943 | goto raise_neg_overflow; 1944 | } 1945 | #endif 1946 | if (sizeof(long) <= sizeof(unsigned long)) { 1947 | __PYX_VERIFY_RETURN_INT_EXC(long, unsigned long, PyLong_AsUnsignedLong(x)) 1948 | #ifdef HAVE_LONG_LONG 1949 | } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { 1950 | __PYX_VERIFY_RETURN_INT_EXC(long, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) 1951 | #endif 1952 | } 1953 | } else { 1954 | #if CYTHON_USE_PYLONG_INTERNALS 1955 | const digit* digits = ((PyLongObject*)x)->ob_digit; 1956 | switch (Py_SIZE(x)) { 1957 | case 0: return (long) 0; 1958 | case -1: __PYX_VERIFY_RETURN_INT(long, sdigit, (sdigit) (-(sdigit)digits[0])) 1959 | case 1: __PYX_VERIFY_RETURN_INT(long, digit, +digits[0]) 1960 | case -2: 1961 | if (8 * sizeof(long) - 1 > 1 * PyLong_SHIFT) { 1962 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 1963 | __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1964 | } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { 1965 | return (long) (((long)-1)*(((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 1966 | } 1967 | } 1968 | break; 1969 | case 2: 1970 | if (8 * sizeof(long) > 1 * PyLong_SHIFT) { 1971 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 1972 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1973 | } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { 1974 | return (long) ((((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 1975 | } 1976 | } 1977 | break; 1978 | case -3: 1979 | if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { 1980 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 1981 | __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1982 | } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { 1983 | return (long) (((long)-1)*(((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 1984 | } 1985 | } 1986 | break; 1987 | case 3: 1988 | if (8 * sizeof(long) > 2 * PyLong_SHIFT) { 1989 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 1990 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 1991 | } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { 1992 | return (long) ((((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 1993 | } 1994 | } 1995 | break; 1996 | case -4: 1997 | if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { 1998 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 1999 | __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2000 | } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { 2001 | return (long) (((long)-1)*(((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2002 | } 2003 | } 2004 | break; 2005 | case 4: 2006 | if (8 * sizeof(long) > 3 * PyLong_SHIFT) { 2007 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2008 | __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2009 | } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { 2010 | return (long) ((((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); 2011 | } 2012 | } 2013 | break; 2014 | } 2015 | #endif 2016 | if (sizeof(long) <= sizeof(long)) { 2017 | __PYX_VERIFY_RETURN_INT_EXC(long, long, PyLong_AsLong(x)) 2018 | #ifdef HAVE_LONG_LONG 2019 | } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { 2020 | __PYX_VERIFY_RETURN_INT_EXC(long, PY_LONG_LONG, PyLong_AsLongLong(x)) 2021 | #endif 2022 | } 2023 | } 2024 | { 2025 | #if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) 2026 | PyErr_SetString(PyExc_RuntimeError, 2027 | "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); 2028 | #else 2029 | long val; 2030 | PyObject *v = __Pyx_PyNumber_IntOrLong(x); 2031 | #if PY_MAJOR_VERSION < 3 2032 | if (likely(v) && !PyLong_Check(v)) { 2033 | PyObject *tmp = v; 2034 | v = PyNumber_Long(tmp); 2035 | Py_DECREF(tmp); 2036 | } 2037 | #endif 2038 | if (likely(v)) { 2039 | int one = 1; int is_little = (int)*(unsigned char *)&one; 2040 | unsigned char *bytes = (unsigned char *)&val; 2041 | int ret = _PyLong_AsByteArray((PyLongObject *)v, 2042 | bytes, sizeof(val), 2043 | is_little, !is_unsigned); 2044 | Py_DECREF(v); 2045 | if (likely(!ret)) 2046 | return val; 2047 | } 2048 | #endif 2049 | return (long) -1; 2050 | } 2051 | } else { 2052 | long val; 2053 | PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); 2054 | if (!tmp) return (long) -1; 2055 | val = __Pyx_PyInt_As_long(tmp); 2056 | Py_DECREF(tmp); 2057 | return val; 2058 | } 2059 | raise_overflow: 2060 | PyErr_SetString(PyExc_OverflowError, 2061 | "value too large to convert to long"); 2062 | return (long) -1; 2063 | raise_neg_overflow: 2064 | PyErr_SetString(PyExc_OverflowError, 2065 | "can't convert negative value to long"); 2066 | return (long) -1; 2067 | } 2068 | 2069 | /* CIntFromPy */ 2070 | static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) { 2071 | const int neg_one = (int) ((int) 0 - (int) 1), const_zero = (int) 0; 2072 | const int is_unsigned = neg_one > const_zero; 2073 | #if PY_MAJOR_VERSION < 3 2074 | if (likely(PyInt_Check(x))) { 2075 | if (sizeof(int) < sizeof(long)) { 2076 | __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG(x)) 2077 | } else { 2078 | long val = PyInt_AS_LONG(x); 2079 | if (is_unsigned && unlikely(val < 0)) { 2080 | goto raise_neg_overflow; 2081 | } 2082 | return (int) val; 2083 | } 2084 | } else 2085 | #endif 2086 | if (likely(PyLong_Check(x))) { 2087 | if (is_unsigned) { 2088 | #if CYTHON_USE_PYLONG_INTERNALS 2089 | const digit* digits = ((PyLongObject*)x)->ob_digit; 2090 | switch (Py_SIZE(x)) { 2091 | case 0: return (int) 0; 2092 | case 1: __PYX_VERIFY_RETURN_INT(int, digit, digits[0]) 2093 | case 2: 2094 | if (8 * sizeof(int) > 1 * PyLong_SHIFT) { 2095 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2096 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2097 | } else if (8 * sizeof(int) >= 2 * PyLong_SHIFT) { 2098 | return (int) (((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); 2099 | } 2100 | } 2101 | break; 2102 | case 3: 2103 | if (8 * sizeof(int) > 2 * PyLong_SHIFT) { 2104 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2105 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2106 | } else if (8 * sizeof(int) >= 3 * PyLong_SHIFT) { 2107 | return (int) (((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); 2108 | } 2109 | } 2110 | break; 2111 | case 4: 2112 | if (8 * sizeof(int) > 3 * PyLong_SHIFT) { 2113 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2114 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2115 | } else if (8 * sizeof(int) >= 4 * PyLong_SHIFT) { 2116 | return (int) (((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); 2117 | } 2118 | } 2119 | break; 2120 | } 2121 | #endif 2122 | #if CYTHON_COMPILING_IN_CPYTHON 2123 | if (unlikely(Py_SIZE(x) < 0)) { 2124 | goto raise_neg_overflow; 2125 | } 2126 | #else 2127 | { 2128 | int result = PyObject_RichCompareBool(x, Py_False, Py_LT); 2129 | if (unlikely(result < 0)) 2130 | return (int) -1; 2131 | if (unlikely(result == 1)) 2132 | goto raise_neg_overflow; 2133 | } 2134 | #endif 2135 | if (sizeof(int) <= sizeof(unsigned long)) { 2136 | __PYX_VERIFY_RETURN_INT_EXC(int, unsigned long, PyLong_AsUnsignedLong(x)) 2137 | #ifdef HAVE_LONG_LONG 2138 | } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) { 2139 | __PYX_VERIFY_RETURN_INT_EXC(int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) 2140 | #endif 2141 | } 2142 | } else { 2143 | #if CYTHON_USE_PYLONG_INTERNALS 2144 | const digit* digits = ((PyLongObject*)x)->ob_digit; 2145 | switch (Py_SIZE(x)) { 2146 | case 0: return (int) 0; 2147 | case -1: __PYX_VERIFY_RETURN_INT(int, sdigit, (sdigit) (-(sdigit)digits[0])) 2148 | case 1: __PYX_VERIFY_RETURN_INT(int, digit, +digits[0]) 2149 | case -2: 2150 | if (8 * sizeof(int) - 1 > 1 * PyLong_SHIFT) { 2151 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2152 | __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2153 | } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { 2154 | return (int) (((int)-1)*(((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2155 | } 2156 | } 2157 | break; 2158 | case 2: 2159 | if (8 * sizeof(int) > 1 * PyLong_SHIFT) { 2160 | if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { 2161 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2162 | } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { 2163 | return (int) ((((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2164 | } 2165 | } 2166 | break; 2167 | case -3: 2168 | if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { 2169 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2170 | __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2171 | } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { 2172 | return (int) (((int)-1)*(((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2173 | } 2174 | } 2175 | break; 2176 | case 3: 2177 | if (8 * sizeof(int) > 2 * PyLong_SHIFT) { 2178 | if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { 2179 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2180 | } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { 2181 | return (int) ((((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2182 | } 2183 | } 2184 | break; 2185 | case -4: 2186 | if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { 2187 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2188 | __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2189 | } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { 2190 | return (int) (((int)-1)*(((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2191 | } 2192 | } 2193 | break; 2194 | case 4: 2195 | if (8 * sizeof(int) > 3 * PyLong_SHIFT) { 2196 | if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { 2197 | __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) 2198 | } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { 2199 | return (int) ((((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); 2200 | } 2201 | } 2202 | break; 2203 | } 2204 | #endif 2205 | if (sizeof(int) <= sizeof(long)) { 2206 | __PYX_VERIFY_RETURN_INT_EXC(int, long, PyLong_AsLong(x)) 2207 | #ifdef HAVE_LONG_LONG 2208 | } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) { 2209 | __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x)) 2210 | #endif 2211 | } 2212 | } 2213 | { 2214 | #if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) 2215 | PyErr_SetString(PyExc_RuntimeError, 2216 | "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); 2217 | #else 2218 | int val; 2219 | PyObject *v = __Pyx_PyNumber_IntOrLong(x); 2220 | #if PY_MAJOR_VERSION < 3 2221 | if (likely(v) && !PyLong_Check(v)) { 2222 | PyObject *tmp = v; 2223 | v = PyNumber_Long(tmp); 2224 | Py_DECREF(tmp); 2225 | } 2226 | #endif 2227 | if (likely(v)) { 2228 | int one = 1; int is_little = (int)*(unsigned char *)&one; 2229 | unsigned char *bytes = (unsigned char *)&val; 2230 | int ret = _PyLong_AsByteArray((PyLongObject *)v, 2231 | bytes, sizeof(val), 2232 | is_little, !is_unsigned); 2233 | Py_DECREF(v); 2234 | if (likely(!ret)) 2235 | return val; 2236 | } 2237 | #endif 2238 | return (int) -1; 2239 | } 2240 | } else { 2241 | int val; 2242 | PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); 2243 | if (!tmp) return (int) -1; 2244 | val = __Pyx_PyInt_As_int(tmp); 2245 | Py_DECREF(tmp); 2246 | return val; 2247 | } 2248 | raise_overflow: 2249 | PyErr_SetString(PyExc_OverflowError, 2250 | "value too large to convert to int"); 2251 | return (int) -1; 2252 | raise_neg_overflow: 2253 | PyErr_SetString(PyExc_OverflowError, 2254 | "can't convert negative value to int"); 2255 | return (int) -1; 2256 | } 2257 | 2258 | /* FastTypeChecks */ 2259 | #if CYTHON_COMPILING_IN_CPYTHON 2260 | static int __Pyx_InBases(PyTypeObject *a, PyTypeObject *b) { 2261 | while (a) { 2262 | a = a->tp_base; 2263 | if (a == b) 2264 | return 1; 2265 | } 2266 | return b == &PyBaseObject_Type; 2267 | } 2268 | static CYTHON_INLINE int __Pyx_IsSubtype(PyTypeObject *a, PyTypeObject *b) { 2269 | PyObject *mro; 2270 | if (a == b) return 1; 2271 | mro = a->tp_mro; 2272 | if (likely(mro)) { 2273 | Py_ssize_t i, n; 2274 | n = PyTuple_GET_SIZE(mro); 2275 | for (i = 0; i < n; i++) { 2276 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) 2277 | return 1; 2278 | } 2279 | return 0; 2280 | } 2281 | return __Pyx_InBases(a, b); 2282 | } 2283 | #if PY_MAJOR_VERSION == 2 2284 | static int __Pyx_inner_PyErr_GivenExceptionMatches2(PyObject *err, PyObject* exc_type1, PyObject* exc_type2) { 2285 | PyObject *exception, *value, *tb; 2286 | int res; 2287 | __Pyx_PyThreadState_declare 2288 | __Pyx_PyThreadState_assign 2289 | __Pyx_ErrFetch(&exception, &value, &tb); 2290 | res = exc_type1 ? PyObject_IsSubclass(err, exc_type1) : 0; 2291 | if (unlikely(res == -1)) { 2292 | PyErr_WriteUnraisable(err); 2293 | res = 0; 2294 | } 2295 | if (!res) { 2296 | res = PyObject_IsSubclass(err, exc_type2); 2297 | if (unlikely(res == -1)) { 2298 | PyErr_WriteUnraisable(err); 2299 | res = 0; 2300 | } 2301 | } 2302 | __Pyx_ErrRestore(exception, value, tb); 2303 | return res; 2304 | } 2305 | #else 2306 | static CYTHON_INLINE int __Pyx_inner_PyErr_GivenExceptionMatches2(PyObject *err, PyObject* exc_type1, PyObject *exc_type2) { 2307 | int res = exc_type1 ? __Pyx_IsSubtype((PyTypeObject*)err, (PyTypeObject*)exc_type1) : 0; 2308 | if (!res) { 2309 | res = __Pyx_IsSubtype((PyTypeObject*)err, (PyTypeObject*)exc_type2); 2310 | } 2311 | return res; 2312 | } 2313 | #endif 2314 | static int __Pyx_PyErr_GivenExceptionMatchesTuple(PyObject *exc_type, PyObject *tuple) { 2315 | Py_ssize_t i, n; 2316 | assert(PyExceptionClass_Check(exc_type)); 2317 | n = PyTuple_GET_SIZE(tuple); 2318 | #if PY_MAJOR_VERSION >= 3 2319 | for (i=0; ip) { 2377 | #if PY_MAJOR_VERSION < 3 2378 | if (t->is_unicode) { 2379 | *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); 2380 | } else if (t->intern) { 2381 | *t->p = PyString_InternFromString(t->s); 2382 | } else { 2383 | *t->p = PyString_FromStringAndSize(t->s, t->n - 1); 2384 | } 2385 | #else 2386 | if (t->is_unicode | t->is_str) { 2387 | if (t->intern) { 2388 | *t->p = PyUnicode_InternFromString(t->s); 2389 | } else if (t->encoding) { 2390 | *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL); 2391 | } else { 2392 | *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); 2393 | } 2394 | } else { 2395 | *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); 2396 | } 2397 | #endif 2398 | if (!*t->p) 2399 | return -1; 2400 | if (PyObject_Hash(*t->p) == -1) 2401 | return -1; 2402 | ++t; 2403 | } 2404 | return 0; 2405 | } 2406 | 2407 | static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) { 2408 | return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str)); 2409 | } 2410 | static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject* o) { 2411 | Py_ssize_t ignore; 2412 | return __Pyx_PyObject_AsStringAndSize(o, &ignore); 2413 | } 2414 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 2415 | #if !CYTHON_PEP393_ENABLED 2416 | static const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2417 | char* defenc_c; 2418 | PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); 2419 | if (!defenc) return NULL; 2420 | defenc_c = PyBytes_AS_STRING(defenc); 2421 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2422 | { 2423 | char* end = defenc_c + PyBytes_GET_SIZE(defenc); 2424 | char* c; 2425 | for (c = defenc_c; c < end; c++) { 2426 | if ((unsigned char) (*c) >= 128) { 2427 | PyUnicode_AsASCIIString(o); 2428 | return NULL; 2429 | } 2430 | } 2431 | } 2432 | #endif 2433 | *length = PyBytes_GET_SIZE(defenc); 2434 | return defenc_c; 2435 | } 2436 | #else 2437 | static CYTHON_INLINE const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2438 | if (unlikely(__Pyx_PyUnicode_READY(o) == -1)) return NULL; 2439 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2440 | if (likely(PyUnicode_IS_ASCII(o))) { 2441 | *length = PyUnicode_GET_LENGTH(o); 2442 | return PyUnicode_AsUTF8(o); 2443 | } else { 2444 | PyUnicode_AsASCIIString(o); 2445 | return NULL; 2446 | } 2447 | #else 2448 | return PyUnicode_AsUTF8AndSize(o, length); 2449 | #endif 2450 | } 2451 | #endif 2452 | #endif 2453 | static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { 2454 | #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 2455 | if ( 2456 | #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 2457 | __Pyx_sys_getdefaultencoding_not_ascii && 2458 | #endif 2459 | PyUnicode_Check(o)) { 2460 | return __Pyx_PyUnicode_AsStringAndSize(o, length); 2461 | } else 2462 | #endif 2463 | #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE)) 2464 | if (PyByteArray_Check(o)) { 2465 | *length = PyByteArray_GET_SIZE(o); 2466 | return PyByteArray_AS_STRING(o); 2467 | } else 2468 | #endif 2469 | { 2470 | char* result; 2471 | int r = PyBytes_AsStringAndSize(o, &result, length); 2472 | if (unlikely(r < 0)) { 2473 | return NULL; 2474 | } else { 2475 | return result; 2476 | } 2477 | } 2478 | } 2479 | static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { 2480 | int is_true = x == Py_True; 2481 | if (is_true | (x == Py_False) | (x == Py_None)) return is_true; 2482 | else return PyObject_IsTrue(x); 2483 | } 2484 | static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject* x) { 2485 | int retval; 2486 | if (unlikely(!x)) return -1; 2487 | retval = __Pyx_PyObject_IsTrue(x); 2488 | Py_DECREF(x); 2489 | return retval; 2490 | } 2491 | static PyObject* __Pyx_PyNumber_IntOrLongWrongResultType(PyObject* result, const char* type_name) { 2492 | #if PY_MAJOR_VERSION >= 3 2493 | if (PyLong_Check(result)) { 2494 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, 2495 | "__int__ returned non-int (type %.200s). " 2496 | "The ability to return an instance of a strict subclass of int " 2497 | "is deprecated, and may be removed in a future version of Python.", 2498 | Py_TYPE(result)->tp_name)) { 2499 | Py_DECREF(result); 2500 | return NULL; 2501 | } 2502 | return result; 2503 | } 2504 | #endif 2505 | PyErr_Format(PyExc_TypeError, 2506 | "__%.4s__ returned non-%.4s (type %.200s)", 2507 | type_name, type_name, Py_TYPE(result)->tp_name); 2508 | Py_DECREF(result); 2509 | return NULL; 2510 | } 2511 | static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x) { 2512 | #if CYTHON_USE_TYPE_SLOTS 2513 | PyNumberMethods *m; 2514 | #endif 2515 | const char *name = NULL; 2516 | PyObject *res = NULL; 2517 | #if PY_MAJOR_VERSION < 3 2518 | if (likely(PyInt_Check(x) || PyLong_Check(x))) 2519 | #else 2520 | if (likely(PyLong_Check(x))) 2521 | #endif 2522 | return __Pyx_NewRef(x); 2523 | #if CYTHON_USE_TYPE_SLOTS 2524 | m = Py_TYPE(x)->tp_as_number; 2525 | #if PY_MAJOR_VERSION < 3 2526 | if (m && m->nb_int) { 2527 | name = "int"; 2528 | res = m->nb_int(x); 2529 | } 2530 | else if (m && m->nb_long) { 2531 | name = "long"; 2532 | res = m->nb_long(x); 2533 | } 2534 | #else 2535 | if (likely(m && m->nb_int)) { 2536 | name = "int"; 2537 | res = m->nb_int(x); 2538 | } 2539 | #endif 2540 | #else 2541 | if (!PyBytes_CheckExact(x) && !PyUnicode_CheckExact(x)) { 2542 | res = PyNumber_Int(x); 2543 | } 2544 | #endif 2545 | if (likely(res)) { 2546 | #if PY_MAJOR_VERSION < 3 2547 | if (unlikely(!PyInt_Check(res) && !PyLong_Check(res))) { 2548 | #else 2549 | if (unlikely(!PyLong_CheckExact(res))) { 2550 | #endif 2551 | return __Pyx_PyNumber_IntOrLongWrongResultType(res, name); 2552 | } 2553 | } 2554 | else if (!PyErr_Occurred()) { 2555 | PyErr_SetString(PyExc_TypeError, 2556 | "an integer is required"); 2557 | } 2558 | return res; 2559 | } 2560 | static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) { 2561 | Py_ssize_t ival; 2562 | PyObject *x; 2563 | #if PY_MAJOR_VERSION < 3 2564 | if (likely(PyInt_CheckExact(b))) { 2565 | if (sizeof(Py_ssize_t) >= sizeof(long)) 2566 | return PyInt_AS_LONG(b); 2567 | else 2568 | return PyInt_AsSsize_t(b); 2569 | } 2570 | #endif 2571 | if (likely(PyLong_CheckExact(b))) { 2572 | #if CYTHON_USE_PYLONG_INTERNALS 2573 | const digit* digits = ((PyLongObject*)b)->ob_digit; 2574 | const Py_ssize_t size = Py_SIZE(b); 2575 | if (likely(__Pyx_sst_abs(size) <= 1)) { 2576 | ival = likely(size) ? digits[0] : 0; 2577 | if (size == -1) ival = -ival; 2578 | return ival; 2579 | } else { 2580 | switch (size) { 2581 | case 2: 2582 | if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { 2583 | return (Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2584 | } 2585 | break; 2586 | case -2: 2587 | if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { 2588 | return -(Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2589 | } 2590 | break; 2591 | case 3: 2592 | if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { 2593 | return (Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2594 | } 2595 | break; 2596 | case -3: 2597 | if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { 2598 | return -(Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2599 | } 2600 | break; 2601 | case 4: 2602 | if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { 2603 | return (Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2604 | } 2605 | break; 2606 | case -4: 2607 | if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { 2608 | return -(Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); 2609 | } 2610 | break; 2611 | } 2612 | } 2613 | #endif 2614 | return PyLong_AsSsize_t(b); 2615 | } 2616 | x = PyNumber_Index(b); 2617 | if (!x) return -1; 2618 | ival = PyInt_AsSsize_t(x); 2619 | Py_DECREF(x); 2620 | return ival; 2621 | } 2622 | static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b) { 2623 | return b ? __Pyx_NewRef(Py_True) : __Pyx_NewRef(Py_False); 2624 | } 2625 | static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) { 2626 | return PyInt_FromSize_t(ival); 2627 | } 2628 | 2629 | 2630 | #endif /* Py_PYTHON_H */ 2631 | -------------------------------------------------------------------------------- /Ch07/07_05/relu.cpython-38-x86_64-linux-gnu.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch07/07_05/relu.cpython-38-x86_64-linux-gnu.so -------------------------------------------------------------------------------- /Ch07/07_05/relu.pyx: -------------------------------------------------------------------------------- 1 | #cython: language_level=3 2 | 3 | def relu(double n): 4 | if n < 0: 5 | return 0 6 | return n 7 | -------------------------------------------------------------------------------- /Ch07/07_05/relu1.py: -------------------------------------------------------------------------------- 1 | def relu(n): 2 | if n < 0: 3 | return 0 4 | return n 5 | -------------------------------------------------------------------------------- /Ch07/07_05/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from Cython.Build import cythonize 3 | 4 | setup( 5 | ext_modules=cythonize('relu.pyx'), 6 | ) 7 | -------------------------------------------------------------------------------- /Ch07/challenge/second.py: -------------------------------------------------------------------------------- 1 | def second(values): 2 | top, second = values[0], values[1] 3 | for value in values[2:]: 4 | if value > top: 5 | top, second = value, top 6 | elif value > second: 7 | second = value 8 | return second 9 | -------------------------------------------------------------------------------- /Ch07/solution/second.py: -------------------------------------------------------------------------------- 1 | def second(values): 2 | top, second = values[0], values[1] 3 | for value in values[2:]: 4 | if value > top: 5 | top, second = value, top 6 | elif value > second: 7 | second = value 8 | return second 9 | -------------------------------------------------------------------------------- /Ch08/08_02/logs-1.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/08_02/logs-1.csv.xz -------------------------------------------------------------------------------- /Ch08/08_02/logs-2.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/08_02/logs-2.csv.xz -------------------------------------------------------------------------------- /Ch08/08_02/logs-3.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/08_02/logs-3.csv.xz -------------------------------------------------------------------------------- /Ch08/08_02/logs-4.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/08_02/logs-4.csv.xz -------------------------------------------------------------------------------- /Ch08/08_02/logs-5.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/08_02/logs-5.csv.xz -------------------------------------------------------------------------------- /Ch08/08_03/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/08_03/taxi.csv.xz -------------------------------------------------------------------------------- /Ch08/challenge/mean_dist.py: -------------------------------------------------------------------------------- 1 | """Using vaex, calculate the mean taxi ride distance in "taxi.csv.xz" per 2 | VendorID. 3 | """ 4 | -------------------------------------------------------------------------------- /Ch08/challenge/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/challenge/taxi.csv.xz -------------------------------------------------------------------------------- /Ch08/solution/mean_dist.py: -------------------------------------------------------------------------------- 1 | """Using vaex, calculate the mean taxi ride distance in "taxi.csv.xz" per 2 | VendorID. 3 | """ 4 | 5 | import vaex 6 | 7 | df = vaex.read_csv('taxi.csv.xz') 8 | out = df.groupby('VendorID', vaex.agg.mean(df['trip_distance'])) 9 | print(out) 10 | -------------------------------------------------------------------------------- /Ch08/solution/taxi.csv.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/faster-pandas-2832038/02ac4fe09cdf7c67e71d50272f6aeb2127a0e0ec/Ch08/solution/taxi.csv.xz -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2019 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Faster pandas 2 | This is the repository for the LinkedIn Learning course Faster pandas. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![Faster pandas][lil-thumbnail-url] 5 | Data scientists often favor pandas, because it lets them work efficiently with larger amounts of data—a useful quality as data sets become bigger and bigger. In this course, instructor Miki Tebeka shows you how to improve your pandas’ code’s speed and efficiency. First, Miki explains why performance matters and how you can measure it with Python profilers. Then, the course teaches you how to use vectorization to manipulate data. The course also walks through some common mistakes and how to address them. 6 | 7 | Python and pandas have many high-performance built-in functions, and Miki covers how to use them. Pandas can use a lot of memory, so Miki offers good tips on how to save memory. The course demonstrates how to serialize data with SQL and HDF5. Then Miki goes over how to speed up your code with Numba and Cython. Alternative DataFrames can also speed up your code, and Miki steps through some options. Plus, explore a few extra resources that you can check out. 8 | 9 | ## Instructions 10 | This repository has branches for each of the videos in the course. You can use the branch pop up menu in github to switch to a specific branch and take a look at the course at that stage, or you can add `/tree/BRANCH_NAME` to the URL to go to the branch you want to access. 11 | 12 | ## Branches 13 | The branches are structured to correspond to the videos in the course. The naming convention is `CHAPTER#_MOVIE#`. As an example, the branch named `02_03` corresponds to the second chapter and the third video in that chapter. 14 | Some branches will have a beginning and an end state. These are marked with the letters `b` for "beginning" and `e` for "end". The `b` branch contains the code as it is at the beginning of the movie. The `e` branch contains the code as it is at the end of the movie. The `main` branch holds the final state of the code when in the course. 15 | 16 | ## Installing 17 | 1. To use these exercise files, you must have the following installed: 18 | - Python 3.6 and up 19 | - To install packages used, run `python -m pip install -r requirements.txt` 20 | 2. Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree. 21 | 3. If you want to follow along, install [IPython](https://ipython.org/) 22 | - `python -m pip install ipython` 23 | 24 | ### Instructor 25 | 26 | **Mike Tebeka** 27 | 28 | _CEO at 353Solutions_ 29 | 30 | Check out some of my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/miki-tebeka). 31 | 32 | [lil-course-url]: https://www.linkedin.com/learning/faster-pandas 33 | [lil-thumbnail-url]: https://cdn.lynda.com/course/2832038/2832038-1605286392196-16x9.jpg 34 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Python Requirements 2 | 3 | # You can install all the requirements with the following shell command 4 | # python -m pip install -r requirements.txt 5 | 6 | Cython~=0.29 7 | dask[complete]~=2.2 8 | ipython~=8.10 9 | numba~=0.5 10 | numexpr~=2.7 11 | pandas~=1.1 12 | pytest-benchmark~=3.2 13 | tables~=3.6 14 | --------------------------------------------------------------------------------