NL: send a signal `signal.sigusr1` to the current process ref: os.kill(os.getpid(), signal.SIGUSR1) Output: proc.terminate() bleu_metric: 8.15663954186223 ---------- NL: decode a hex string '4a4b4c' to utf-8. ref: bytes.fromhex('4a4b4c').decode('utf-8') Output: print(str(a.encode('hexB', 'hex')) bleu_metric: 13.947196297357516 ---------- NL: check if all elements in list `mylist` are identical ref: all(x == myList[0] for x in myList) Output: len(set(mylist)) == 1 bleu_metric: 9.360791398475978 ---------- NL: format number of spaces between strings `python`, `:` and `very good` to be `20` ref: print('%*s : %*s' % (20, 'Python', 20, 'Very Good')) Output: """{:,}""".format(a) bleu_metric: 7.039722936059098 ---------- NL: how to convert a string from cp-1251 to utf-8? ref: d.decode('cp1251').encode('utf8') Output: urllib.parse.unquote(s.encode('utf8')) bleu_metric: 47.38611152748619 ---------- NL: get rid of none values in dictionary `kwargs` ref: res = {k: v for k, v in list(kwargs.items()) if v is not None} Output: {k: v for k, v in list(d.items()) if v > 0} bleu_metric: 64.58251510057832 ---------- NL: get rid of none values in dictionary `kwargs` ref: res = dict((k, v) for k, v in kwargs.items() if v is not None) Output: {k: v for k, v in list(d.items()) if v > 0} bleu_metric: 32.55630358189704 ---------- NL: capture final output of a chain of system commands `ps -ef | grep something | wc -l` ref: subprocess.check_output('ps -ef | grep something | wc -l', shell=True) Output: subprocess.call('echo "foo"', shell=True) bleu_metric: 33.54121358422117 ---------- NL: concatenate a list of strings `['a', 'b', 'c']` ref: """""".join(['a', 'b', 'c']) Output: print(('a', 'b', 'c')) bleu_metric: 42.80768209469581 ---------- NL: find intersection data between series `s1` and series `s2` ref: pd.Series(list(set(s1).intersection(set(s2)))) Output: s1 = pd.DataFrame(s1, s2=None) bleu_metric: 10.722703083793286 ---------- NL: sending http headers to `client` ref: client.send('HTTP/1.0 200 OK\r\n') Output: s.send('HTTP/1.0 200 OK\r\n') bleu_metric: 94.57416090031758 ---------- NL: format a datetime string `when` to extract date only ref: then = datetime.datetime.strptime(when, '%Y-%m-%d').date() Output: dt.strftime('%m/%d/%Y') bleu_metric: 10.693730801587432 ---------- NL: split a multi-line string `inputstring` into separate strings ref: inputString.split('\n') Output: a, b, c = input('string_escape') bleu_metric: 21.972813874997158 ---------- NL: split a multi-line string ` a \n b \r\n c ` by new line character `\n` ref: ' a \n b \r\n c '.split('\n') Output: a.rstrip().split('\n') bleu_metric: 38.90205926838098 ---------- NL: concatenate elements of list `b` by a colon ":" ref: """:""".join(str(x) for x in b) Output: print(repr(a)) bleu_metric: 4.9422115544674705 ---------- NL: get the first object from a queryset in django model `entry` ref: Entry.objects.filter()[:1].get() Output: Entry.objects.get_values('id') bleu_metric: 23.929967984761056 ---------- NL: calculate sum over all rows of 2d numpy array ref: a.sum(axis=1) Output: np.sum(a.shape[1:]) bleu_metric: 24.022110864391532 ---------- NL: enable warnings using action 'always' ref: warnings.simplefilter('always') Output: os.kill(logging.DEBUG) bleu_metric: 21.105340631872636 ---------- NL: concatenate items of list `l` with a space ' ' ref: print(' '.join(map(str, l))) Output: print(' '.join(map(str, l)) bleu_metric: 91.69729150999534 ---------- NL: run script 'hello.py' with argument 'htmlfilename.htm' on terminal using python executable ref: subprocess.call(['python.exe', 'hello.py', 'htmlfilename.htm']) Output: subprocess.call('start "$file.py', shell=True) bleu_metric: 24.391589282046496 ---------- NL: how can i parse a time string containing milliseconds in it with python? ref: time.strptime('30/03/09 16:31:32.123', '%d/%m/%y %H:%M:%S.%f') Output: time.strftime('%H:%M:%S,%f')) bleu_metric: 19.792633055621124 ---------- NL: convert a string `my_string` with dot and comma into a float number `my_float` ref: my_float = float(my_string.replace(',', '')) Output: my_string.replace(',', '') bleu_metric: 62.69839476367552 ---------- NL: convert a string `123,456.908` with dot and comma into a floating number ref: float('123,456.908'.replace(',', '')) Output: print(', '.join(str(12345)) bleu_metric: 21.93127055132894 ---------- NL: set pythonpath in python script. ref: sys.path.append('/path/to/whatever') Output: os.path.abspath(__file__) bleu_metric: 14.91608246051739 ---------- NL: split string 'words, words, words.' using a regex '(\\w+)' ref: re.split('(\\W+)', 'Words, words, words.') Output: re.split('\\b(\\w+)', 'Words, words, words.') bleu_metric: 71.4022021970798 ---------- NL: open a file `output.txt` in append mode ref: file = open('Output.txt', 'a') Output: f = open('file.txt', 'r') bleu_metric: 54.7332024239071 ---------- NL: download a file "http://www.example.com/songs/mp3.mp3" over http and save to "mp3.mp3" ref: urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3') Output: urllib.request.urlretrieve('http://randomsite.com/', 'rb').read() bleu_metric: 45.9127788668896 ---------- NL: download a file `url` over http and save to `file_name` ref: u = urllib.request.urlopen(url) Output: file_name = wget.urlretrieve('http://example.com/file.gz') bleu_metric: 9.449865252803164 ---------- NL: download a file 'http://www.example.com/' over http ref: response = urllib.request.urlopen('http://www.example.com/') html = response.read() Output: urllib.request.urlretrieve('http://www.com/file.com/file.gz') bleu_metric: 41.91356463630533 ---------- NL: download a file `url` over http ref: r = requests.get(url) Output: urllib.request.urlretrieve('http://example.com/file.com/file.gz') bleu_metric: 7.265857755970445 ---------- NL: download a file `url` over http and save to "10mb" ref: response = requests.get(url, stream=True) Output: urllib.request.urlretrieve('http://example.com/file.gz', 'file.gz') bleu_metric: 7.029695662739608 ---------- NL: argparse add argument with flag '--version' and version action of '%(prog)s 2.0' to parser `parser` ref: parser.add_argument('--version', action='version', version='%(prog)s 2.0') Output: parser.add_argument('--conf', nargs='*') bleu_metric: 24.648885368725924 ---------- NL: remove key 'c' from dictionary `d` ref: {i: d[i] for i in d if i != 'c'} Output: d = {'a': 1, 'c': 2, 'c': 3, 'c': 3} bleu_metric: 10.884267615693712 ---------- NL: create new dataframe object by merging columns "key" of dataframes `split_df` and `csv_df` and rename the columns from dataframes `split_df` and `csv_df` with suffix `_left` and `_right` respectively ref: pd.merge(split_df, csv_df, on=['key'], suffixes=('_left', '_right')) Output: pd.concat([df_1, df_2.columns], axis=1, on=['A_B', 'B' bleu_metric: 24.624806058504372 ---------- NL: split a string `s` by space with `4` splits ref: s.split(' ', 4) Output: re.split('\\s+|\\S+', s) bleu_metric: 23.58844810653421 ---------- NL: read keyboard-input ref: input('Enter your input:') Output: sys.stdout.flush() bleu_metric: 17.6625510283176 ---------- NL: enable debug mode on flask application `app` ref: app.run(debug=True) Output: app.run(debug=True) bleu_metric: 100.0 ---------- NL: python save list `mylist` to file object 'save.txt' ref: pickle.dump(mylist, open('save.txt', 'wb')) Output: file.write('myfile.txt', 'wb') bleu_metric: 42.60520663282873 ---------- NL: multiply a matrix `p` with a 3d tensor `t` in scipy ref: scipy.tensordot(P, T, axes=[1, 1]).swapaxes(0, 1) Output: np.dot(x.reshape(-1, 3), np.dot(0, 1, 0, 1)) bleu_metric: 22.78371390626513 ---------- NL: create 3d array of zeroes of size `(3,3,3)` ref: numpy.zeros((3, 3, 3)) Output: np.zeros((3, 3)) bleu_metric: 75.33808072882879 ---------- NL: cut off the last word of a sentence `content` ref: """ """.join(content.split(' ')[:-1]) Output: """ """.join(s.split()) bleu_metric: 46.40451719480771 ---------- NL: convert scalar `x` to array ref: x = np.asarray(x).reshape(1, -1)[(0), :] Output: np.zeros((x, x) for x in s) bleu_metric: 9.777229794850028 ---------- NL: sum all elements of nested list `l` ref: sum(sum(i) if isinstance(i, list) else i for i in L) Output: sum(lst) bleu_metric: 1.841640459320245 ---------- NL: convert hex string '470fc614' to a float number ref: struct.unpack('!f', '470FC614'.decode('hex'))[0] Output: struct.unpack('d', struct.pack('0x1252', 'F6d0424a'))[0] bleu_metric: 48.34492705061849 ---------- NL: multiple each value by `2` for all keys in a dictionary `my_dict` ref: my_dict.update((x, y * 2) for x, y in list(my_dict.items())) Output: dict((k, v) for k, v in my_dict.items()) bleu_metric: 28.26550613903659 ---------- NL: running bash script 'sleep.sh' ref: subprocess.call('sleep.sh', shell=True) Output: subprocess.call('start command -c 'r', shell=True) bleu_metric: 55.7853798004205 ---------- NL: join elements of list `l` with a comma `,` ref: """,""".join(l) Output: [y for x in L for y in x if x.startswith(''.join(',')] bleu_metric: 22.50265947708922 ---------- NL: make a comma-separated string from a list `mylist` ref: myList = ','.join(map(str, myList)) Output: myList = mylist[:n] bleu_metric: 10.218289380194193 ---------- NL: reverse the list that contains 1 to 10 ref: list(reversed(list(range(10)))) Output: L[::-1] bleu_metric: 7.575149194183216 ---------- NL: remove substring 'bag,' from a string 'lamp, bag, mirror' ref: print('lamp, bag, mirror'.replace('bag,', '')) Output: [m.strip() for m in x if m.endswith('@')] bleu_metric: 10.616735292999413 ---------- NL: reverse the order of words, delimited by `.`, in string `s` ref: """.""".join(s.split('.')[::-1]) Output: sorted(s, key=lambda s: s.split('.') if s else s s[1])) bleu_metric: 35.65843931149688 ---------- NL: convert epoch time represented as milliseconds `s` to string using format '%y-%m-%d %h:%m:%s.%f' ref: datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f') Output: print(s.strftime('%Y-%m-%d %H:%M:%S')) bleu_metric: 62.56418230541474 ---------- NL: parse milliseconds epoch time '1236472051807' to format '%y-%m-%d %h:%m:%s' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1236472051807 / 1000.0)) Output: time.strftime('%Y-%m-%d %H:%M:%S') bleu_metric: 59.71758060527691 ---------- NL: get the date 7 days before the current date ref: (datetime.datetime.now() - datetime.timedelta(days=7)).date() Output: (datetime.date(2010, 12, 31) + relativedelta(months=(+ 1))) bleu_metric: 23.713320246552005 ---------- NL: sum elements at index `column` of each list in list `data` ref: print(sum(row[column] for row in data)) Output: data[sum(b) for b in zip(*data)] bleu_metric: 15.844501337268929 ---------- NL: sum columns of a list `array` ref: [sum(row[i] for row in array) for i in range(len(array[0]))] Output: sum(sum(row[:, (1)] for row in mylist) bleu_metric: 21.03312756130989 ---------- NL: encode binary string 'your string' to base64 code ref: base64.b64encode(bytes('your string', 'utf-8')) Output: print(content.encode('base64')) bleu_metric: 20.291424899466545 ---------- NL: combine list of dictionaries `dicts` with the same keys in each list to a single dictionary ref: dict((k, [d[k] for d in dicts]) for k in dicts[0]) Output: dict((k, v) for d in dicts for k, v in list(d.items()) if k in d] bleu_metric: 28.548259613272815 ---------- NL: merge a nested dictionary `dicts` into a flat dictionary by concatenating nested values with the same key `k` ref: {k: [d[k] for d in dicts] for k in dicts[0]} Output: dict((k, v) for k, v in list(d.items())) bleu_metric: 8.608775825594615 ---------- NL: how do i get the url parameter in a flask view ref: request.args['myParam'] Output: request.url bleu_metric: 6.856953606192477 ---------- NL: identify duplicate values in list `mylist` ref: [k for k, v in list(Counter(mylist).items()) if v > 1] Output: set(mylist)) bleu_metric: 2.0605095345578897 ---------- NL: insert directory 'apps' into directory `__file__` ref: sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps')) Output: os.chdir(path) bleu_metric: 1.4431184819037772 ---------- NL: modify sys.path for python module `subdir` ref: sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) Output: my_path.append('/path/to/my_file.txt') bleu_metric: 10.285502024406256 ---------- NL: insert a 'none' value into a sqlite3 table. ref: db.execute("INSERT INTO present VALUES('test2', ?, 10)", (None,)) Output: cursor.execute('insert into example values values values)') bleu_metric: 14.095580788435447 ---------- NL: flatten list `list_of_menuitems` ref: [image for menuitem in list_of_menuitems for image in menuitem] Output: list_of_lists = [list(f) for f in my_list] bleu_metric: 14.949751774990682 ---------- NL: append elements of a set `b` to a list `a` ref: a.extend(b) Output: a.append(b) bleu_metric: 59.46035575013605 ---------- NL: append elements of a set to a list in python ref: a.extend(list(b)) Output: a.extend(b) bleu_metric: 46.81294641285289 ---------- NL: write the data of dataframe `df` into text file `np.txt` ref: np.savetxt('c:\\data\\np.txt', df.values, fmt='%d') Output: df.write.txt', header=None, delimiter='\t') bleu_metric: 17.503492126188856 ---------- NL: write content of dataframe `df` into text file 'c:\\data\\pandas.txt' ref: df.to_csv('c:\\data\\pandas.txt', header=None, index=None, sep=' ', mode='a') Output: df.to_csv('c:\\data\\data\\t.txt', sep='\t') bleu_metric: 45.493235468867745 ---------- NL: split a string `x` by last occurrence of character `-` ref: print(x.rpartition('-')[0]) Output: x.split('\\d+', s) bleu_metric: 16.460024863157845 ---------- NL: get the last part of a string before the character '-' ref: print(x.rsplit('-', 1)[0]) Output: print(x.rfind('-')[0]) bleu_metric: 60.109065739327065 ---------- NL: upload file using ftp ref: ftp.storlines('STOR ' + filename, open(filename, 'r')) Output: ftp.quit() bleu_metric: 3.710176016036221 ---------- NL: add one to the hidden web element with id 'xyz' with selenium python script ref: browser.execute_script("document.getElementById('XYZ').value+='1'") Output: driver.find_element_by_xpath("//li/label/input[contains(..,'polishpottery')]") bleu_metric: 13.25451214611751 ---------- NL: create array containing the maximum value of respective elements of array `[2, 3, 4]` and array `[1, 5, 2]` ref: np.maximum([2, 3, 4], [1, 5, 2]) Output: np.array([1, 2, 3], [2, 3, 4], [3, 4, 5]]) bleu_metric: 42.651255482235264 ---------- NL: print a list `l` and move first 3 elements to the end of the list ref: print(l[3:] + l[:3]) Output: print(l[:5] + l[0]) bleu_metric: 50.02460338549947 ---------- NL: loop over files in directory '.' ref: for fn in os.listdir('.'): Output: for os.path.join(path, filename) bleu_metric: 16.07716496330015 ---------- NL: loop over files in directory `source` ref: for (root, dirs, filenames) in os.walk(source): Output: for os.path.join(path, filename) bleu_metric: 11.519792081464464 ---------- NL: create a random list of integers ref: [int(1000 * random.random()) for i in range(10000)] Output: randomList = [random.random() for _ in range(10)] bleu_metric: 34.3414769312466 ---------- NL: using %f with strftime() in python to get microseconds ref: datetime.datetime.now().strftime('%H:%M:%S.%f') Output: datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') bleu_metric: 65.15366202853991 ---------- NL: google app engine execute gql query 'select * from schedule where station = $1' with parameter `foo.key()` ref: db.GqlQuery('SELECT * FROM Schedule WHERE station = $1', foo.key()) Output: c.execute('SELECT * FROM foo WHERE bar = %s AND baz = %s', (param1, param2)) bleu_metric: 24.090844358935918 ---------- NL: filter rows in pandas starting with alphabet 'f' using regular expression. ref: df.b.str.contains('^f') Output: df.dropna(lambda x: x.replace('\\[^\\w\\s]', 'A') bleu_metric: 10.727295782787309 ---------- NL: print a 2 dimensional list `tab` as a table with delimiters ref: print('\n'.join('\t'.join(str(col) for col in row) for row in tab)) Output: print('\n'.join(map(str, row)) bleu_metric: 26.894705347686966 ---------- NL: pandas: delete rows in dataframe `df` based on multiple columns values ref: df.set_index(list('BC')).drop(tuples, errors='ignore').reset_index() Output: df.drop(df.index[[1, 3]], axis=1) bleu_metric: 11.446641525222896 ---------- NL: format the variables `self.goals` and `self.penalties` using string formatting ref: """({:d} goals, ${:d})""".format(self.goals, self.penalties) Output: """{0} {1}""".format(name='bar', 'change!') bleu_metric: 28.1073423324667 ---------- NL: format string "({} goals, ${})" with variables `goals` and `penalties` ref: """({} goals, ${})""".format(self.goals, self.penalties) Output: """{} {}""".format(TotalAmount) bleu_metric: 30.199592877917368 ---------- NL: format string "({0.goals} goals, ${0.penalties})" ref: """({0.goals} goals, ${0.penalties})""".format(self) Output: """{0} {1}""".format(name, 20) bleu_metric: 32.37295639418319 ---------- NL: convert list of lists `l` to list of integers ref: [int(''.join(str(d) for d in x)) for x in L] Output: [int(x) for x in L] bleu_metric: 26.91716452870884 ---------- NL: combine elements of each list in list `l` into digits of a single integer ref: [''.join(str(d) for d in x) for x in L] Output: int(''.join(map(str, list)) bleu_metric: 23.011232462942058 ---------- NL: convert a list of lists `l` to list of integers ref: L = [int(''.join([str(y) for y in x])) for x in L] Output: [int(x) for x in L] bleu_metric: 18.058610039563465 ---------- NL: write the elements of list `lines` concatenated by special character '\n' to file `myfile` ref: myfile.write('\n'.join(lines)) Output: with open('myfile.txt', 'w').read().write('\n') bleu_metric: 30.77874158297155 ---------- NL: removing an element from a list based on a predicate 'x' or 'n' ref: [x for x in ['AAT', 'XAC', 'ANT', 'TTA'] if 'X' not in x and 'N' not in x] Output: [x for x in L if x is not None] bleu_metric: 7.979312731505724 ---------- NL: remove duplicate words from a string `text` using regex ref: text = re.sub('\\b(\\w+)( \\1\\b)+', '\\1', text) Output: re.sub('(?<=[a-z])\\r?\\w+', '\\1', s) bleu_metric: 43.796510807866646 ---------- NL: count non zero values in each column in pandas data frame ref: df.astype(bool).sum(axis=1) Output: df.apply(lambda x: 0).sum() if sum(x) > 1) bleu_metric: 23.20305803246989 ---------- NL: search for string that matches regular expression pattern '(?.*<' in xml string `line` ref: print(re.search('>.*<', line).group(0)) Output: print(re.search('(?<=\\w+)', line)) bleu_metric: 47.71512105691513 ---------- NL: erase all the contents of a file `filename` ref: open(filename, 'w').close() Output: open('filename', 'w').close() bleu_metric: 70.98108717509044 ---------- NL: convert a string into datetime using the format '%y-%m-%d %h:%m:%s.%f' ref: datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S.%f') Output: datetime.strptime('2010-11-13 10:33:54.227806', '%Y-%m-%d bleu_metric: 40.88633662916028 ---------- NL: find the index of a list with the first element equal to '332' within the list of lists `thelist` ref: [index for index, item in enumerate(thelist) if item[0] == '332'] Output: [i for i, v in enumerate(L) if v[0] == 'id' in x] bleu_metric: 33.034466035625115 ---------- NL: lower a string `text` and remove non-alphanumeric characters aside from space ref: re.sub('[^\\sa-zA-Z0-9]', '', text).lower().strip() Output: re.sub('[^\\w-zA-Z0-9]','', text) bleu_metric: 64.45968952353843 ---------- NL: remove all non-alphanumeric characters except space from a string `text` and lower it ref: re.sub('(?!\\s)[\\W_]', '', text).lower().strip() Output: re.sub('[^0-9a-zA-Z0-9]', '', text) bleu_metric: 34.05969875105766 ---------- NL: subscript text 'h20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='H\u2082O') Output: plt.show() bleu_metric: 6.117046115680378 ---------- NL: subscript text 'h20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='$H_2O$') Output: plt.show() bleu_metric: 6.117046115680378 ---------- NL: loop over a list `mylist` if sublists length equals 3 ref: [x for x in mylist if len(x) == 3] Output: [(x + 1) for x in mylist] bleu_metric: 27.97803653491597 ---------- NL: initialize a list `lst` of 100 objects object() ref: lst = [Object() for _ in range(100)] Output: [item for item in lst] bleu_metric: 11.786767588753086 ---------- NL: create list `lst` containing 100 instances of object `object` ref: lst = [Object() for i in range(100)] Output: [x for x in lst if x is not None] bleu_metric: 13.386745323687673 ---------- NL: get the content of child tag with`href` attribute whose parent has css `someclass` ref: self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') Output: soup.find_all('a', {'id':'st,'st_name':'st/content': 'content'} bleu_metric: 7.478822222889464 ---------- NL: joining data from dataframe `df1` with data from dataframe `df2` based on matching values of column 'date_time' in both dataframes ref: df1.merge(df2, on='Date_Time') Output: pd.merge(df1, df2, how='inner', on=['user_id']) bleu_metric: 18.70222963555319 ---------- NL: use `%s` operator to print variable values `str1` inside a string ref: 'first string is: %s, second one is: %s' % (str1, 'geo.tif') Output: print('{0}'.format(num)) bleu_metric: 5.68097120736594 ---------- NL: split a string by a delimiter in python ref: [x.strip() for x in '2.MATCHES $$TEXT$$ STRING'.split('$$TEXT$$')] Output: a.split('=') bleu_metric: 2.904501569386027 ---------- NL: check if directory `directory ` exists and create it if necessary ref: if (not os.path.exists(directory)): os.makedirs(directory) Output: if (not os.path.exists(directory)): os.makedirs(directory) bleu_metric: 100.0 ---------- NL: check if a directory `path` exists and create it if necessary ref: distutils.dir_util.mkpath(path) Output: os.makedirs(path) bleu_metric: 40.661103887968814 ---------- NL: check if a directory `path` exists and create it if necessary ref: distutils.dir_util.mkpath(path) Output: os.makedirs(path) bleu_metric: 40.661103887968814 ---------- NL: check if a directory `path` exists and create it if necessary ref: os.makedirs(path) Output: os.makedirs(path) bleu_metric: 100.0 ---------- NL: replace a separate word 'h3' by 'h1' in a string 'text' ref: re.sub('\\bH3\\b', 'H1', text) Output: re.sub('(?= 99) & (df['closing_price'] <= 101)] Output: df[df['column_name'].str[1:] bleu_metric: 9.220008234645237 ---------- NL: replace all occurences of newlines `\n` with `
` in dataframe `df` ref: df.replace({'\n': '
'}, regex=True) Output: df.replace('\n', '>').replace('\r\n') bleu_metric: 28.577933090423212 ---------- NL: replace all occurrences of a string `\n` by string `
` in a pandas data frame `df` ref: df.replace({'\n': '
'}, regex=True) Output: df.replace({'\n': '
'}, regex=True) bleu_metric: 100.0 ---------- NL: create a list containing each two adjacent letters in string `word` as its elements ref: [(x + y) for x, y in zip(word, word[1:])] Output: print(list(itertools.chain.from_iterable([''.join([''.join(i)) for i in myList bleu_metric: 6.9872837779786146 ---------- NL: get a list of pairs from a string `word` using lambda function ref: list(map(lambda x, y: x + y, word[:-1], word[1:])) Output: [item[0] for item in wordids] bleu_metric: 3.6045344658126863 ---------- NL: extract a url from a string `mystring` ref: print(re.findall('(https?://[^\\s]+)', myString)) Output: print(re.search('(?<=\\S+)', mystring)) bleu_metric: 29.098751790827798 ---------- NL: extract a url from a string `mystring` ref: print(re.search('(?Phttps?://[^\\s]+)', myString).group('url')) Output: print(re.search('(?<=\\S+)', mystring)) bleu_metric: 26.19504694379959 ---------- NL: remove all special characters, punctuation and spaces from a string `mystring` using regex ref: re.sub('[^A-Za-z0-9]+', '', mystring) Output: re.sub('[^\\w' ]", '', mystring) bleu_metric: 54.67160268053085 ---------- NL: create a datetimeindex containing 13 periods of the second friday of each month starting from date '2016-01-01' ref: pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13) Output: datetime.datetime.strptime('01-01-04', '%d-%b-%Y') bleu_metric: 15.911783110981517 ---------- NL: create multidimensional array `matrix` with 3 rows and 2 columns in python ref: matrix = [[a, b], [c, d], [e, f]] Output: np.array([[0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 1 bleu_metric: 11.802861352393501 ---------- NL: replace spaces with underscore ref: mystring.replace(' ', '_') Output: re.sub('\\([^\\w\\)', '', text) bleu_metric: 15.604242268653643 ---------- NL: get an absolute file path of file 'mydir/myfile.txt' ref: os.path.abspath('mydir/myfile.txt') Output: os.path.abspath(mypath) bleu_metric: 36.304072644520666 ---------- NL: split string `my_string` on white spaces ref: """ """.join(my_string.split()) Output: my_string.split('_') bleu_metric: 19.77851893672361 ---------- NL: get filename without extension from file `filename` ref: os.path.splitext(filename)[0] Output: os.path.splitext('/path/to/filename.txt') bleu_metric: 33.70129264673147 ---------- NL: get a list containing the sum of each element `i` in list `l` plus the previous elements ref: [sum(l[:i]) for i, _ in enumerate(l)] Output: [sum(x) for x in lst if x > 2] bleu_metric: 20.61396573920308 ---------- NL: split a string `docs/src/scripts/temp` by `/` keeping `/` in the result ref: """Docs/src/Scripts/temp""".replace('/', '/\x00/').split('\x00') Output: result = subprocess.Popen(sys.+)" -DOL) bleu_metric: 3.19149448369712 ---------- NL: shuffle columns of an numpy array 'r' ref: np.random.shuffle(np.transpose(r)) Output: np.random.shuffle(np.random.randint(0, 1, axis=1)) bleu_metric: 41.3948387915005 ---------- NL: copy all values in a column 'b' to a new column 'd' in a pandas data frame 'df' ref: df['D'] = df['B'] Output: df['B'] = df['B'] bleu_metric: 80.03203203844998 ---------- NL: find a value within nested json 'data' where the key inside another key 'b' is unknown. ref: list(data['A']['B'].values())[0]['maindata'][0]['Info'] Output: data[(key_data['Date']['post code']) & (data['Date'])] bleu_metric: 21.54701803718216 ---------- NL: check characters of string `string` are true predication of function `predicate` ref: all(predicate(x) for x in string) Output: all(x in string for x in list(string.items()) if x == 'isdit') bleu_metric: 16.27934873162478 ---------- NL: determine number of files on a drive with python ref: os.statvfs('/').f_files - os.statvfs('/').f_ffree Output: os.stat(path).st_common(f.st_f) bleu_metric: 10.276795791404941 ---------- NL: how to get a single result from a sqlite query in python? ref: cursor.fetchone()[0] Output: cursor.execute('User')[0] bleu_metric: 44.08231875586727 ---------- NL: convert string `user_input` into a list of integers `user_list` ref: user_list = [int(number) for number in user_input.split(',')] Output: return int(soup.split(',')) bleu_metric: 34.34281712615472 ---------- NL: get a list of integers by splitting a string `user` with comma ref: [int(s) for s in user.split(',')] Output: [int(x) for x in [1, 2, 3, 4, 5] bleu_metric: 21.79301929852717 ---------- NL: sorting a python list by two criteria ref: sorted(list, key=lambda x: (x[0], -x[1])) Output: sorted(list, key=lambda x: (x[1], x[0])) bleu_metric: 80.44410811781832 ---------- NL: sort a list of objects `ut`, based on a function `cmpfun` in descending order ref: ut.sort(key=cmpfun, reverse=True) Output: sorted(a, key=lambda x: x.count(x.getter(1)) bleu_metric: 10.813005337959174 ---------- NL: reverse list `ut` based on the `count` attribute of each object ref: ut.sort(key=lambda x: x.count, reverse=True) Output: sum(lst) bleu_metric: 2.867683507302 ---------- NL: sort a list of objects `ut` in reverse order by their `count` property ref: ut.sort(key=lambda x: x.count, reverse=True) Output: sorted(L, key=lambda x: (-len(x.split('-))) bleu_metric: 25.0737833894674 ---------- NL: click a href button 'send' with selenium ref: driver.find_element_by_partial_link_text('Send').click() Output: browser.find_element_by_text('http://www.example.com') bleu_metric: 14.44788670919441 ---------- NL: click a href button having text `send inmail` with selenium ref: driver.findElement(By.linkText('Send InMail')).click() Output: browser.find_element_by_class_name('http://www.com/') bleu_metric: 10.573773767686836 ---------- NL: click a href button with text 'send inmail' with selenium ref: driver.find_element_by_link_text('Send InMail').click() Output: browser.find_element_by_class_name('http://www.text.com') bleu_metric: 14.44788670919441 ---------- NL: cast an int `i` to a string and concat to string 'me' ref: 'ME' + str(i) Output: """""".join(i) bleu_metric: 31.61487584488943 ---------- NL: sorting data in dataframe pandas ref: df.sort_values(['System_num', 'Dis']) Output: df.sort('m') bleu_metric: 13.487005099534619 ---------- NL: prepend the line '#test firstline\n' to the contents of file 'infile' and save as the file 'outfile' ref: open('outfile', 'w').write('#test firstline\n' + open('infile').read()) Output: open('filename', 'w').write('\n').read().write('\n').write('\n') bleu_metric: 45.445331007629186 ---------- NL: sort a list `l` by length of value in tuple ref: l.sort(key=lambda t: len(t[1]), reverse=True) Output: print(sorted(lst, key=lambda tup: len(x), len(y))) bleu_metric: 17.729842264695016 ---------- NL: split string `s` by words that ends with 'd' ref: re.findall('\\b(\\w+)d\\b', s) Output: [word for word in s.split('.')[0] for s in s.split()] bleu_metric: 7.987276352377326 ---------- NL: return `true` if string `foobarrrr` contains regex `ba[rzd]` ref: bool(re.search('ba[rzd]', 'foobarrrr')) Output: re.search('(?<=.)(?=(a))', 'aaa bbb ccc ddd e') bleu_metric: 23.770841795864293 ---------- NL: removing duplicates in list `t` ref: list(set(t)) Output: list(set(item) for item in L)) bleu_metric: 35.556702356686955 ---------- NL: removing duplicates in list `source_list` ref: list(set(source_list)) Output: list(item) bleu_metric: 24.795364698947967 ---------- NL: removing duplicates in list `abracadabra` ref: list(OrderedDict.fromkeys('abracadabra')) Output: list(set(t)) bleu_metric: 19.745471508497758 ---------- NL: convert array `a` into a list ref: numpy.array(a).reshape(-1).tolist() Output: a.append(a) bleu_metric: 9.852859740626553 ---------- NL: convert the first row of numpy matrix `a` to a list ref: numpy.array(a)[0].tolist() Output: numpy.array(a, a[0]) bleu_metric: 44.579146390257485 ---------- NL: in `soup`, get the content of the sibling of the `td` tag with text content `address:` ref: print(soup.find(text='Address:').findNext('td').contents[0]) Output: soup.findAll(text='"') bleu_metric: 12.1241604438414 ---------- NL: convert elements of each tuple in list `l` into a string separated by character `@` ref: """ """.join([('%d@%d' % t) for t in l]) Output: """""".join(map(lambda x: x[1]) bleu_metric: 37.15744930678825 ---------- NL: convert each tuple in list `l` to a string with '@' separating the tuples' elements ref: """ """.join([('%d@%d' % (t[0], t[1])) for t in l]) Output: [(x + y) for x, y in zip(l1, l2)] bleu_metric: 4.866055054993489 ---------- NL: get the html from the current web page of a selenium driver ref: driver.execute_script('return document.documentElement.outerHTML;') Output: driver.get('http://www.google.com') bleu_metric: 19.209534151258666 ---------- NL: get all matches with regex pattern `\\d+[xx]` in list of string `teststr` ref: [i for i in teststr if re.search('\\d+[xX]', i)] Output: [m.start() for m in re.findall('\\d+', 'abcd')] bleu_metric: 31.35886387911532 ---------- NL: select values from column 'a' for which corresponding values in column 'b' will be greater than 50, and in column 'c' - equal 900 in dataframe `df` ref: df['A'][(df['B'] > 50) & (df['C'] == 900)] Output: df[['A', 'B']].fillna(df['A'] bleu_metric: 32.2826123723905 ---------- NL: sort dictionary `o` in ascending order based on its keys and items ref: sorted(o.items()) Output: sorted(list(d.items()), key=lambda v: v[1]) bleu_metric: 24.42195895839763 ---------- NL: get sorted list of keys of dict `d` ref: sorted(d) Output: sorted(d, key=d.get) bleu_metric: 25.21193618434983 ---------- NL: how to sort dictionaries by keys in python ref: sorted(d.items()) Output: sorted(d.items()), key=lambda x: (x['name'], x['name'], x['name'])) bleu_metric: 21.62882016096312 ---------- NL: convert string "1" into integer ref: int('1') Output: int('1', 2) bleu_metric: 58.143073696821936 ---------- NL: function to convert strings into integers ref: int() Output: int(''.join(map(str, x))) bleu_metric: 12.729922658368398 ---------- NL: convert items in `t1` to integers ref: T2 = [map(int, x) for x in T1] Output: int('1') bleu_metric: 7.448668213629092 ---------- NL: call a shell script `./test.sh` using subprocess ref: subprocess.call(['./test.sh']) Output: subprocess.call('test.sh otherfunc') bleu_metric: 34.64490516910983 ---------- NL: call a shell script `notepad` using subprocess ref: subprocess.call(['notepad']) Output: subprocess.call(['notepad']) bleu_metric: 100.0 ---------- NL: combine lists `l1` and `l2` by alternating their elements ref: [val for pair in zip(l1, l2) for val in pair] Output: [(x + y) for x, y in zip(l1, l2)] bleu_metric: 40.59264050928986 ---------- NL: encode string 'data to be encoded' ref: encoded = base64.b64encode('data to be encoded') Output: data = json.encode('utf-8') bleu_metric: 21.665290989148364 ---------- NL: encode a string `data to be encoded` to `ascii` encoding ref: encoded = 'data to be encoded'.encode('ascii') Output: data = 'data2' bleu_metric: 6.5298221612721505 ---------- NL: parse tab-delimited csv file 'text.txt' into a list ref: lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t')) Output: open('demo.txt', 'r').read() bleu_metric: 18.96773811885661 ---------- NL: get attribute `my_str` of object `my_object` ref: getattr(my_object, my_str) Output: my_obj.__dict__ bleu_metric: 11.909345107359583 ---------- NL: group a list of dicts `ld` into one dict by key ref: print(dict(zip(LD[0], zip(*[list(d.values()) for d in LD])))) Output: dict((k, v) for k, v in list(d.items()) for k, v in list(d.items())} bleu_metric: 21.161547096559342 ---------- NL: how do i sum the first value in each tuple in a list of tuples in python? ref: sum([pair[0] for pair in list_of_pairs]) Output: sum(item[0] for item in mylist) bleu_metric: 32.21542689451684 ---------- NL: convert unicode string u"{'code1':1,'code2':1}" into dictionary ref: d = ast.literal_eval("{'code1':1,'code2':1}") Output: {'MyString1': '1', '1'}).encode('ascii', 'ignore') bleu_metric: 14.294565332608489 ---------- NL: find all words in a string `mystring` that start with the `$` sign ref: [word for word in mystring.split() if word.startswith('$')] Output: re.findall('\\[^\\w\\w', mystring) bleu_metric: 9.573479881751316 ---------- NL: remove any url within string `text` ref: text = re.sub('^https?:\\/\\/.*[\\r\\n]*', '', text, flags=re.MULTILINE) Output: text = re.sub('\\r\\r\\r\\n*\\Z', '', text) bleu_metric: 39.07206775645457 ---------- NL: replace all elements in array `a` that are not present in array `[1, 3, 4]` with zeros ref: np.where(np.in1d(A, [1, 3, 4]).reshape(A.shape), A, 0) Output: np.isnan(a).any(np.in1d(a), 0, a) bleu_metric: 20.39361671090609 ---------- NL: calculate mean across dimension in a 2d array `a` ref: np.mean(a, axis=1) Output: np.mean(a, axis=1) bleu_metric: 100.0 ---------- NL: running r script '/pathto/myrscript.r' from python ref: subprocess.call(['/usr/bin/Rscript', '--vanilla', '/pathto/MyrScript.r']) Output: sys.exit(app.exec_()) bleu_metric: 2.642308696142247 ---------- NL: run r script '/usr/bin/rscript --vanilla /pathto/myrscript.r' ref: subprocess.call('/usr/bin/Rscript --vanilla /pathto/MyrScript.r', shell=True) Output: subprocess.call('/usr/bin/perl', shell=True) bleu_metric: 48.72501457287311 ---------- NL: add a header to a csv file ref: writer.writeheader() Output: writer.writeheader() bleu_metric: 100.0 ---------- NL: replacing nan in the dataframe `df` with row average ref: df.fillna(df.mean(axis=1), axis=1) Output: df.fillna(df.mean(axis=1), axis=0) bleu_metric: 86.6397551781362 ---------- NL: convert unix timestamp '1347517370' to formatted string '%y-%m-%d %h:%m:%s' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) Output: datetime.strptime('21/11/13 19:19:30 -0400', '%Y/%m/%d bleu_metric: 12.446578619659645 ---------- NL: call a base class's class method `do` from derived class `derived` ref: super(Derived, cls).do(a) Output: super(MyClass, self).__init__(*args, **kwargs) bleu_metric: 14.02577516008147 ---------- NL: selecting rows in numpy ndarray 'a', where the value in the first column is 0 and value in the second column is 1 ref: a[np.where((a[:, (0)] == 0) * (a[:, (1)] == 1))] Output: a[np.arange(a[0])!= 0] bleu_metric: 10.315793886636692 ---------- NL: separate words delimited by one or more spaces into a list ref: re.split(' +', 'hello world sample text') Output: [word for word in my_list if any(x) not in '\n' % x.split()] bleu_metric: 10.332090908268508 ---------- NL: length of longest element in list `words` ref: len(max(words, key=len)) Output: len(words, key=lambda i: len(word) for word in word_list) bleu_metric: 30.51327880362441 ---------- NL: get the value associated with unicode key 'from_user' of first dictionary in list `result` ref: result[0]['from_user'] Output: [x for x in list_of_dict if x['success'] not in data] bleu_metric: 12.879862858915844 ---------- NL: retrieve each line from a file 'file.txt' as a list ref: [line.split() for line in open('File.txt')] Output: line = [line.strip() for line in open('filename.txt')] bleu_metric: 63.83964846132485 ---------- NL: swap keys with values in a dictionary `a` ref: res = dict((v, k) for k, v in a.items()) Output: dict((v, k) for k, v in a.items()) bleu_metric: 90.00876262522594 ---------- NL: open a file `path/to/file_name.ext` in write mode ref: new_file = open('path/to/FILE_NAME.ext', 'w') Output: shutil.copyfile('/to/file.txt', 'path') bleu_metric: 25.66441742647156 ---------- NL: how to count distinct values in a column of a pandas group by object? ref: df.groupby(['col1', 'col2'])['col3'].nunique().reset_index() Output: df.groupby('A').agg(lambda x: x.nlargest(x.name).nunique()) bleu_metric: 24.81076483975817 ---------- NL: check if any key in the dictionary `dict1` starts with the string `emp$$` ref: any(key.startswith('EMP$$') for key in dict1) Output: any(dict.values()) bleu_metric: 9.663861439684919 ---------- NL: create list of values from dictionary `dict1` that have a key that starts with 'emp$$' ref: [value for key, value in list(dict1.items()) if key.startswith('EMP$$')] Output: [key for key, value in list(d.items()) if 'new y' in d] bleu_metric: 43.11804919179004 ---------- NL: convert a pandas series `sf` into a pandas dataframe `df` with columns `email` and `list` ref: pd.DataFrame({'email': sf.index, 'list': sf.values}) Output: pd.concat([df1, df2], axis=1) bleu_metric: 8.12657367724027 ---------- NL: print elements of list `list` seperated by tabs `\t` ref: print('\t'.join(map(str, list))) Output: print(' '.join(map(list, list))) bleu_metric: 62.705622531832795 ---------- NL: print unicode string '\xd0\xbf\xd1\x80\xd0\xb8' with utf-8 ref: print('\xd0\xbf\xd1\x80\xd0\xb8'.encode('raw_unicode_escape')) Output: print('\xd0\xd0\xd1\xd0\xd0\xd0\xd0\xd0\xd0\ bleu_metric: 32.278364081487986 ---------- NL: encode a latin character in string `sopet\xc3\xb3n` properly ref: 'Sopet\xc3\xb3n'.encode('latin-1').decode('utf-8') Output: print('\xc3\xa9'.encode('utf-8')) bleu_metric: 40.96260380345688 ---------- NL: resized image `image` to width, height of `(x, y)` with filter of `antialias` ref: image = image.resize((x, y), Image.ANTIALIAS) Output: im = Image.imshow(image, 0, cv2.COLOR_rGR2RGB) bleu_metric: 14.564246815114087 ---------- NL: regex, find "n"s only in the middle of string `s` ref: re.findall('n(?<=[^n]n)n+(?=[^n])(?i)', s) Output: re.sub('(?<=n)', '', s) bleu_metric: 19.97638457636558 ---------- NL: display the float `1/3*100` as a percentage ref: print('{0:.0f}%'.format(1.0 / 3 * 100)) Output: int(round(1.5)) bleu_metric: 8.651705771167139 ---------- NL: sort a list of dictionary `mylist` by the key `title` ref: mylist.sort(key=lambda x: x['title']) Output: mylist.sort(key=lambda x: x['title']) bleu_metric: 100.0 ---------- NL: sort a list `l` of dicts by dict value 'title' ref: l.sort(key=lambda x: x['title']) Output: l.sort(key=lambda x: x['title']) bleu_metric: 100.0 ---------- NL: sort a list of dictionaries by the value of keys 'title', 'title_url', 'id' in ascending order. ref: l.sort(key=lambda x: (x['title'], x['title_url'], x['id'])) Output: my_list.sort(key=lambda x: x['title']) bleu_metric: 30.53233854712605 ---------- NL: find 10 largest differences between each respective elements of list `l1` and list `l2` ref: heapq.nlargest(10, range(len(l1)), key=lambda i: abs(l1[i] - l2[i])) Output: print(max(x, key=sum)) bleu_metric: 5.1753387054451085 ---------- NL: beautifulsoup find all 'span' elements in html string `soup` with class of 'stargryb sp' ref: soup.find_all('span', {'class': 'starGryB sp'}) Output: soup.findAll('div', attrs={'name': 'description'}) bleu_metric: 27.858167198909346 ---------- NL: write records in dataframe `df` to table 'test' in schema 'a_schema' ref: df.to_sql('test', engine, schema='a_schema') Output: df.to_csv('test.csv', index=False) bleu_metric: 20.519505687240557 ---------- NL: extract brackets from string `s` ref: brackets = re.sub('[^(){}[\\]]', '', s) Output: re.findall('\\[\\]]', s) bleu_metric: 36.918962549447706 ---------- NL: remove duplicate elements from list 'l' ref: list(dict((x[0], x) for x in L).values()) Output: [set(l) for l in set(lst) for l in L)] bleu_metric: 13.839002470625022 ---------- NL: read a file `file` without newlines ref: [line.rstrip('\n') for line in file] Output: f.close() bleu_metric: 4.4408502586392995 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 9.025970847670083 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 9.025970847670083 ---------- NL: get the position of item 1 in `testlist` ref: for i in [i for (i, x) in enumerate(testlist) if (x == 1)]: pass Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 7.3690983226275435 ---------- NL: get the position of item 1 in `testlist` ref: for i in (i for (i, x) in enumerate(testlist) if (x == 1)): pass Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 8.372912227572577 ---------- NL: get the position of item 1 in `testlist` ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) for i in gen: pass Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 7.018371226772391 ---------- NL: get the position of item `element` in list `testlist` ref: print(testlist.index(element)) Output: print(testlist.index(x)) bleu_metric: 70.16879391277371 ---------- NL: get the position of item `element` in list `testlist` ref: try: print(testlist.index(element)) except ValueError: pass Output: print(testlist.index(x)) bleu_metric: 34.660486300017936 ---------- NL: find the first element of the tuple with the maximum second element in a list of tuples `lis` ref: max(lis, key=lambda item: item[1])[0] Output: max(x[1] for x in L[0]) bleu_metric: 22.078697088920286 ---------- NL: get the item at index 0 from the tuple that has maximum value at index 1 in list `lis` ref: max(lis, key=itemgetter(1))[0] Output: max(a, key=lambda x: x[1]) bleu_metric: 21.186050864016664 ---------- NL: make a delay of 1 second ref: time.sleep(1) Output: time.sleep(1) bleu_metric: 100.0 ---------- NL: convert list of tuples `l` to a string ref: """, """.join('(' + ', '.join(i) + ')' for i in L) Output: """""".join(map(str, L)) bleu_metric: 22.382912241881993 ---------- NL: django set default value of field `b` equal to '0000000' ref: b = models.CharField(max_length=7, default='0000000', editable=False) Output: super(Model, self).save(*args, **kwargs) bleu_metric: 8.756189878973519 ---------- NL: sort lis `list5` in ascending order based on the degrees value of its elements ref: sorted(list5, lambda x: (degree(x), x)) Output: sorted(list(range(len(a))), key=lambda x: (-len(x)), x[1]) bleu_metric: 21.534597645653108 ---------- NL: how do i perform secondary sorting in python? ref: sorted(list5, key=lambda vertex: (degree(vertex), vertex)) Output: sorted(list1, key=lambda x: x[:-1]) bleu_metric: 23.523340210589698 ---------- NL: convert a list into a generator object ref: (n for n in [1, 2, 3, 5]) Output: [(i * i for i in range(10)) for i in range(10)] bleu_metric: 7.707532488755381 ---------- NL: remove elements from list `oldlist` that have an index number mentioned in list `removelist` ref: newlist = [v for i, v in enumerate(oldlist) if i not in removelist] Output: [item for item in my_list if some_condition()] bleu_metric: 9.65619264674583 ---------- NL: open a file `yourfile.txt` in write mode ref: f = open('yourfile.txt', 'w') Output: f = open('file.txt', 'r') bleu_metric: 61.28081331864039 ---------- NL: get attribute 'attr' from object `obj` ref: getattr(obj, 'attr') Output: getattr(obj, 'attr') bleu_metric: 100.0 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to tuple ref: from functools import reduce reduce(lambda a, b: a + b, (('aa',), ('bb',), ('cc',))) Output: tuple(map(tuple, map(int, t)) bleu_metric: 3.3785986651715976 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to list in one line ref: map(lambda a: a[0], (('aa',), ('bb',), ('cc',))) Output: [('a', 'b', 'c'), ('d', 'e', 'e']) for e in l] bleu_metric: 15.687545978719285 ---------- NL: python pandas: how to replace a characters in a column of a dataframe? ref: df['range'].replace(',', '-', inplace=True) Output: df['AB'].replace(',',', '-', inplace=True) bleu_metric: 80.377750806414 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip(*a) bleu_metric: 0.24532394983186231 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip(*a) bleu_metric: 0.24532394983186231 ---------- NL: unzip list `original` ref: result = ([a for (a, b) in original], [b for (a, b) in original]) Output: zip(*main_list) bleu_metric: 0.7985269443272172 ---------- NL: unzip list `original` and return a generator ref: result = ((a for (a, b) in original), (b for (a, b) in original)) Output: [(i, j) for i in range(10)] bleu_metric: 5.292278815391249 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip(*a) bleu_metric: 0.07639466888110501 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` and fill empty results with none ref: map(None, *[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip(*a) bleu_metric: 0.037243160645090284 ---------- NL: encode `decimal('3.9')` to a json string ref: json.dumps(Decimal('3.9')) Output: json.dumps(ord('x1b2\x92\x03\x9\xa2\x8') bleu_metric: 22.115966812339867 ---------- NL: add key "mynewkey" to dictionary `d` with value "mynewvalue" ref: d['mynewkey'] = 'mynewvalue' Output: d['Test'].update({'item': 1, 'item': 4}, {'item3': 1}) bleu_metric: 9.25329498915617 ---------- NL: add key 'a' to dictionary `data` with value 1 ref: data.update({'a': 1, }) Output: data['a'] = 'b' bleu_metric: 17.86189637183548 ---------- NL: add key 'a' to dictionary `data` with value 1 ref: data.update(dict(a=1)) Output: data['a'] = 'b' bleu_metric: 13.078474031415348 ---------- NL: add key 'a' to dictionary `data` with value 1 ref: data.update(a=1) Output: data['a'] = 'b' bleu_metric: 14.323145079400494 ---------- NL: find maximal value in matrix `matrix` ref: max([max(i) for i in matrix]) Output: max(max(x, key=lambda x: x[1])[0] bleu_metric: 12.58220555845937 ---------- NL: round number `answer` to 2 precision after the decimal point ref: answer = str(round(answer, 2)) Output: round(1.005, 2) bleu_metric: 24.5981275183433 ---------- NL: extract ip address from an html string ref: ip = re.findall('[0-9]+(?:\\.[0-9]+){3}', s) Output: print(re.findall('\\d+(?=(\\.com+)\\.com/', s)) bleu_metric: 29.033332017734637 ---------- NL: filter dataframe `df` by values in column `a` that appear more than once ref: df.groupby('A').filter(lambda x: len(x) > 1) Output: df[df['A'] > 1] bleu_metric: 10.739983258001304 ---------- NL: append each line in file `myfile` into a list ref: [x for x in myfile.splitlines() if x != ''] Output: myfile.write('myfile.txt', 'w').write('\n') bleu_metric: 8.899891914341412 ---------- NL: get a list of integers `lst` from a file `filename.txt` ref: lst = map(int, open('filename.txt').readlines()) Output: [int(i) for i in f.split(',')] bleu_metric: 12.313871716496207 ---------- NL: add color bar with image `mappable` to plot `plt` ref: plt.colorbar(mappable=mappable, cax=ax3) Output: plt.savefig('test.png') bleu_metric: 16.449450056138957 ---------- NL: count most frequent 100 words in column 'text' of dataframe `df` ref: Counter(' '.join(df['text']).split()).most_common(100) Output: df.join(word).most_common(word) bleu_metric: 14.732626148274333 ---------- NL: python split a string using regex ref: re.findall('(.+?):(.+?)\\b ?', text) Output: re.split('(?<=\\.\\d+|\\s)', text) bleu_metric: 27.16899549919204 ---------- NL: generate all 2-element subsets of tuple `(1, 2, 3)` ref: list(itertools.combinations((1, 2, 3), 2)) Output: [list(itertools.product([1, 2, 3, 4], [4, 5, 6])) for a in range( bleu_metric: 23.605722704787006 ---------- NL: get a value of datetime.today() in the utc time zone ref: datetime.now(pytz.utc) Output: datetime.datetime.now().utcnow() bleu_metric: 34.38931217657843 ---------- NL: get a new list `list2`by removing empty list from a list of lists `list1` ref: list2 = [x for x in list1 if x != []] Output: list2 = [x for x in list1 if x!= []] bleu_metric: 100.0 ---------- NL: create `list2` to contain the lists from list `list1` excluding the empty lists from `list1` ref: list2 = [x for x in list1 if x] Output: list23 = [(a - b) for a, b in zip(list1, list2)] bleu_metric: 11.13613656101029 ---------- NL: django response with json `data` ref: return HttpResponse(data, mimetype='application/json') Output: json.dumps(data, ensure_ascii=False) bleu_metric: 18.83283134652959 ---------- NL: get all text that is not enclosed within square brackets in string `example_str` ref: re.findall('(.*?)\\[.*?\\]', example_str) Output: """\\[\\]]""".split() bleu_metric: 16.91906599442455 ---------- NL: use a regex to get all text in a string `example_str` that is not surrounded by square brackets ref: re.findall('(.*?)(?:\\[.*?\\]|$)', example_str) Output: re.findall('\\[(.*?\\]\\]', '', str)) bleu_metric: 41.184938137508176 ---------- NL: get whatever is between parentheses as a single match, and any char outside as an individual match in string '(zyx)bc' ref: re.findall('\\(.+?\\)|\\w', '(zyx)bc') Output: re.findall('\\w+(?:\\w+)', '\\1', s) bleu_metric: 38.05918723545142 ---------- NL: match regex '\\((.*?)\\)|(\\w)' with string '(zyx)bc' ref: re.findall('\\((.*?)\\)|(\\w)', '(zyx)bc') Output: re.match('(?<=\\w+)', 'a\n b\n c') bleu_metric: 21.2203664532429 ---------- NL: match multiple regex patterns with the alternation operator `|` in a string `(zyx)bc` ref: re.findall('\\(.*?\\)|\\w', '(zyx)bc') Output: re.findall('(?<=\\w)\\w', 'abcdefg123 and again abc') bleu_metric: 40.304968802021214 ---------- NL: formate each string cin list `elements` into pattern '%{0}%' ref: elements = ['%{0}%'.format(element) for element in elements] Output: print(' '.join(map(str, list))) bleu_metric: 9.154148084605266 ---------- NL: open a background process 'background-process' with arguments 'arguments' ref: subprocess.Popen(['background-process', 'arguments']) Output: subprocess.Popen(['file', '-r', '-r', 'r', 'file']) bleu_metric: 31.65014630070639 ---------- NL: get list of values from dictionary 'mydict' w.r.t. list of keys 'mykeys' ref: [mydict[x] for x in mykeys] Output: [k for d in mylist if'mykey' in d] bleu_metric: 14.807582680058125 ---------- NL: convert list `[('name', 'joe'), ('age', 22)]` into a dictionary ref: dict([('Name', 'Joe'), ('Age', 22)]) Output: dict((name, eval(name)) for name in ['name','sp', 'name']) bleu_metric: 16.475516456048457 ---------- NL: average each two columns of array `data` ref: data.reshape(-1, j).mean(axis=1).reshape(data.shape[0], -1) Output: data.sum(axis=1).sum(axis=1) bleu_metric: 19.628836362845185 ---------- NL: double backslash escape all double quotes in string `s` ref: print(s.encode('unicode-escape').replace('"', '\\"')) Output: result = result.replace('\\', '') bleu_metric: 23.514174149586935 ---------- NL: split a string into a list of words and whitespace ref: re.split('(\\W+)', s) Output: [word for word in mystring.split()] bleu_metric: 15.620412264187214 ---------- NL: plotting stacked barplots on a panda data frame ref: df.plot(kind='barh', stacked=True) Output: df.plot(kind='bar', stacked=True) bleu_metric: 81.53551038173114 ---------- NL: reverse the keys and values in a dictionary `mydictionary` ref: {i[1]: i[0] for i in list(myDictionary.items())} Output: dict((v, k) for k, v in d.items() if k!= 'None') bleu_metric: 15.450079547146164 ---------- NL: finding the index of elements containing substring 'how' and 'what' in a list of strings 'mylist'. ref: [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] Output: [x for x in mylist if 'abc' in x.startswith('start')] bleu_metric: 9.601670492244972 ---------- NL: check if object `obj` is a string ref: isinstance(obj, str) Output: isinstance(obj, str) bleu_metric: 100.0 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance(a, str) bleu_metric: 59.46035575013605 ---------- NL: check if object `o` is a string ref: (type(o) is str) Output: isinstance(a, str) bleu_metric: 23.813694985189848 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance(a, str) bleu_metric: 59.46035575013605 ---------- NL: check if `obj_to_test` is a string ref: isinstance(obj_to_test, str) Output: if (not some_string): pass bleu_metric: 20.556680845025983 ---------- NL: append list `list1` to `list2` ref: list2.extend(list1) Output: list2.append(list2) bleu_metric: 32.17294420803808 ---------- NL: append list `mylog` to `list1` ref: list1.extend(mylog) Output: mylist.append('myD') bleu_metric: 18.850319022747346 ---------- NL: append list `a` to `c` ref: c.extend(a) Output: a.append(b) bleu_metric: 29.071536848410968 ---------- NL: append items in list `mylog` to `list1` ref: for line in mylog: list1.append(line) Output: mylist.append('myD') bleu_metric: 22.84389301518129 ---------- NL: append a tuple of elements from list `a` with indexes '[0][0] [0][2]' to list `b` ref: b.append((a[0][0], a[0][2])) Output: [(a, b, c) for a, b in zip(a, b)] bleu_metric: 9.850589025894903 ---------- NL: initialize `secret_key` in flask config with `your_secret_string ` ref: app.config['SECRET_KEY'] = 'Your_secret_string' Output: app.run() bleu_metric: 9.728049676725325 ---------- NL: unpack a series of tuples in pandas into a dataframe with column names 'out-1' and 'out-2' ref: pd.DataFrame(out.tolist(), columns=['out-1', 'out-2'], index=out.index) Output: pd.DataFrame(df.columns, index=['A', 'B']) bleu_metric: 23.203863908318283 ---------- NL: find the index of an element 'msft' in a list `stocks_list` ref: [x for x in range(len(stocks_list)) if stocks_list[x] == 'MSFT'] Output: zip(*sorted(enumerate(a), key=lambda x: x[1]) bleu_metric: 7.353922010374821 ---------- NL: rotate the xtick labels of matplotlib plot `ax` by `45` degrees to make long labels readable ref: ax.set_xticklabels(labels, rotation=45) Output: ax.set_xlabel('Temperature (\u2103)') bleu_metric: 16.54461999338998 ---------- NL: remove symbols from a string `s` ref: re.sub('[^\\w]', ' ', s) Output: re.sub('[^\\w]', '', s) bleu_metric: 100.0 ---------- NL: get the current directory of a script ref: os.path.basename(os.path.dirname(os.path.realpath(__file__))) Output: os.path.dirname(os.path.abspath(__file__)) bleu_metric: 52.085526428001614 ---------- NL: find octal characters matches from a string `str` using regex ref: print(re.findall("'\\\\[0-7]{1,3}'", str)) Output: re.findall('[^\\w-\\d]', str) bleu_metric: 27.1936649292955 ---------- NL: split string `input` based on occurrences of regex pattern '[ ](?=[a-z]+\\b)' ref: re.split('[ ](?=[A-Z]+\\b)', input) Output: re.split('[a-zA-Z]', ' 0).objects.filter(id=123)) bleu_metric: 13.78592993183041 ---------- NL: make a barplot of data in column `group` of dataframe `df` colour-coded according to list `color` ref: df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r']) Output: df.plot(x, y, 'B') bleu_metric: 1.7617020801492098 ---------- NL: find all matches of regex pattern '([a-fa-f\\d]{32})' in string `data` ref: re.findall('([a-fA-F\\d]{32})', data) Output: print(re.findall('(?=(a.*?)\\b', '\\1', data)) bleu_metric: 29.06614574259767 ---------- NL: get the length of list `my_list` ref: len(my_list) Output: len(my_list) bleu_metric: 100.0 ---------- NL: getting the length of array `l` ref: len(l) Output: len(l) bleu_metric: 100.0 ---------- NL: getting the length of array `s` ref: len(s) Output: len(s) bleu_metric: 100.0 ---------- NL: getting the length of `my_tuple` ref: len(my_tuple) Output: len(my_list) bleu_metric: 45.180100180492246 ---------- NL: getting the length of `my_string` ref: len(my_string) Output: len(my_string) bleu_metric: 100.0 ---------- NL: remove escape character from string "\\a" ref: """\\a""".decode('string_escape') Output: print(' '.join(a) bleu_metric: 14.244775721428216 ---------- NL: replace each 'a' with 'b' and each 'b' with 'a' in the string 'obama' in a single pass. ref: """obama""".replace('a', '%temp%').replace('b', 'a').replace('%temp%', 'b') Output: """a b""".replace('b', 'a', 'baz')) bleu_metric: 34.4125014551951 ---------- NL: remove directory tree '/folder_name' ref: shutil.rmtree('/folder_name') Output: os.makedirs(path) bleu_metric: 18.094495256969623 ---------- NL: create a new column `weekday` in pandas data frame `data` based on the values in column `my_dt` ref: data['weekday'] = data['my_dt'].apply(lambda x: x.weekday()) Output: data['hour'] = df['Date'].apply(lambda x: x.year)) bleu_metric: 54.038193355462695 ---------- NL: reverse sort counter `x` by values ref: sorted(x, key=x.get, reverse=True) Output: sorted(x.sort(x.c)) bleu_metric: 20.35024093540328 ---------- NL: reverse sort counter `x` by value ref: sorted(list(x.items()), key=lambda pair: pair[1], reverse=True) Output: sorted(x, key=x.get) bleu_metric: 8.425741883707015 ---------- NL: append a numpy array 'b' to a numpy array 'a' ref: np.vstack((a, b)) Output: a.reshape(a, -1, a1) bleu_metric: 23.901088824528134 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: print(concatenate((a, b), axis=0)) Output: print(a.shape[1, 3, 4]) bleu_metric: 15.325316503089068 ---------- NL: numpy concatenate two arrays `a` and `b` along the second axis ref: print(concatenate((a, b), axis=1)) Output: print(concatenate((a, b), axis=1)) bleu_metric: 100.0 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: c = np.r_[(a[None, :], b[None, :])] Output: print(a.shape[1, 3, 4]) bleu_metric: 9.146501935450095 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: np.array((a, b)) Output: print(a.shape[1, 3, 4]) bleu_metric: 15.911783110981517 ---------- NL: fetch address information for host 'google.com' ion port 80 ref: print(socket.getaddrinfo('google.com', 80)) Output: socket.send('HTTP/1.0 200 OK') bleu_metric: 15.850829785188195 ---------- NL: add a column 'day' with value 'sat' to dataframe `df` ref: df.xs('sat', level='day', drop_level=False) Output: df['d'].apply(lambda x: 'Y')) bleu_metric: 11.454635531478054 ---------- NL: return a 401 unauthorized in django ref: return HttpResponse('Unauthorized', status=401) Output: return HttpResponse(status=204) bleu_metric: 32.28559902619133 ---------- NL: flask set folder 'wherever' as the default template folder ref: Flask(__name__, template_folder='wherever') Output: app.path.join(__file__, os.dirname(__file__), 'app')) bleu_metric: 10.457995018881574 ---------- NL: how do i insert into t1 (select * from t2) in sqlalchemy? ref: session.execute('INSERT INTO t1 (SELECT * FROM t2)') Output: db.session.commit() bleu_metric: 9.01760872773444 ---------- NL: sort a list of lists 'c2' such that third row comes first ref: c2.sort(key=lambda row: row[2]) Output: sorted(lst, key=lambda x: x[2])) bleu_metric: 32.22538601891173 ---------- NL: sorting a list of lists in python ref: c2.sort(key=lambda row: (row[2], row[1], row[0])) Output: [sorted(item) for item in data] bleu_metric: 3.9177831075306493 ---------- NL: sorting a list of lists in python ref: c2.sort(key=lambda row: (row[2], row[1])) Output: [sorted(item) for item in data] bleu_metric: 5.9428726447571005 ---------- NL: set font `arial` to display non-ascii characters in matplotlib ref: matplotlib.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'}) Output: plt.setp(loc=('\u2022', 'ignore') bleu_metric: 7.494955959649734 ---------- NL: convert datetime column 'date' of pandas dataframe 'df' to ordinal ref: df['date'].apply(lambda x: x.toordinal()) Output: df['date'] = df['date'].dt.strftime('%d%m%Y') bleu_metric: 26.66090188234886 ---------- NL: get html source of selenium webelement `element` ref: element.get_attribute('innerHTML') Output: driver.find_element_by_xpath("//*[@id='submit']/text()") bleu_metric: 12.173115521158184 ---------- NL: get the integer location of a key `bob` in a pandas data frame ref: df.index.get_loc('bob') Output: df.columns = df['state'].get_dict() bleu_metric: 15.76777026245066 ---------- NL: open a 'gnome' terminal from python script and run 'sudo apt-get update' command. ref: os.system('gnome-terminal -e \'bash -c "sudo apt-get update; exec bash"\'') Output: os.system('start "$file"') bleu_metric: 14.726128825673209 ---------- NL: add an item with key 'third_key' and value 1 to an dictionary `my_dict` ref: my_dict.update({'third_key': 1}) Output: default_dict.update({'key1': 1, 'position': 2}) bleu_metric: 35.95818888322724 ---------- NL: declare an array ref: my_list = [] Output: intarray = array('i') bleu_metric: 15.619699684601276 ---------- NL: insert item `12` to a list `my_list` ref: my_list.append(12) Output: my_list.insert(0, k) bleu_metric: 24.446151121745054 ---------- NL: add an entry 'wuggah' at the beginning of list `mylist` ref: myList.insert(0, 'wuggah') Output: a.append('b') bleu_metric: 21.64910073203448 ---------- NL: convert a hex-string representation to actual bytes ref: """\\xF3\\xBE\\x80\\x80""".replace('\\x', '').decode('hex') Output: binascii.unhexlify('\x00\x80')[2:] bleu_metric: 4.9663342844865035 ---------- NL: select the last column of dataframe `df` ref: df[df.columns[-1]] Output: df.drop(df.columns[-1]] bleu_metric: 69.30977286178778 ---------- NL: get the first value from dataframe `df` where column 'letters' is equal to 'c' ref: df.loc[df['Letters'] == 'C', 'Letters'].values[0] Output: df[df['column'] == 'B'] bleu_metric: 25.294817005134707 ---------- NL: converting two lists `[1, 2, 3]` and `[4, 5, 6]` into a matrix ref: np.column_stack(([1, 2, 3], [4, 5, 6])) Output: np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) bleu_metric: 53.858386640481605 ---------- NL: get the type of `i` ref: type(i) Output: print(isinstance(a)) bleu_metric: 22.679164443904003 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: get the type of variable `variable_name` ref: print(type(variable_name)) Output: print(get_name) bleu_metric: 24.795364698947967 ---------- NL: get the 5th item of a generator ref: next(itertools.islice(range(10), 5, 5 + 1)) Output: next(it) bleu_metric: 2.7474047213893544 ---------- NL: print a string `word` with string format ref: print('"{}"'.format(word)) Output: print('Hello, {0}'.format(word)) bleu_metric: 59.978201631280236 ---------- NL: join a list of strings `list` using a space ' ' ref: """ """.join(list) Output: [')','','','','','','','','','','','','','', ' bleu_metric: 5.987016463560546 ---------- NL: create list `y` containing two empty lists ref: y = [[] for n in range(2)] Output: listy = [[] for i in range(3)] bleu_metric: 49.35578819979933 ---------- NL: read a file 'c:/name/mydocuments/numbers' into a list `data` ref: data = [line.strip() for line in open('C:/name/MyDocuments/numbers', 'r')] Output: data = [r[i] for i in open('myfile.txt')] bleu_metric: 20.807540769186392 ---------- NL: delete all occurrences of character 'i' in string 'it is icy' ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: re.sub('i', '', 'I', 'it is icy') bleu_metric: 22.408895662889165 ---------- NL: delete all instances of a character 'i' in a string 'it is icy' ref: re.sub('i', '', 'it is icy') Output: re.sub('i', '', 'it is icy') bleu_metric: 100.0 ---------- NL: delete all characters "i" in string "it is icy" ref: """it is icy""".replace('i', '') Output: re.sub('[^i]', '', 'I', text) bleu_metric: 22.722445488928127 ---------- NL: how to delete all instances of a character in a string in python? ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: """""".join([i for i in range(100) if i!= 'i']) bleu_metric: 59.20060650358378 ---------- NL: drop rows of pandas dataframe `df` having nan in column at index "1" ref: df.dropna(subset=[1]) Output: df.drop(df.index[[1, 3]], inplace=True) bleu_metric: 12.91802583371725 ---------- NL: get elements from list `mylist`, that have a field `n` value 30 ref: [x for x in myList if x.n == 30] Output: myList = [i for i in range(10) if i % 2 == 0] bleu_metric: 12.58220555845937 ---------- NL: converting list of strings `intstringlist` to list of integer `nums` ref: nums = [int(x) for x in intstringlist] Output: [int(x) for x in intstringlist] bleu_metric: 83.9234989944319 ---------- NL: convert list of string numbers into list of integers ref: map(int, eval(input('Enter the unfriendly numbers: '))) Output: [int(i) for i in str_list] bleu_metric: 7.290792403968443 ---------- NL: print "." without newline ref: sys.stdout.write('.') Output: print('\n'.join(str(x)) bleu_metric: 15.911783110981517 ---------- NL: round off the float that is the product of `2.52 * 100` and convert it to an int ref: int(round(2.51 * 100)) Output: int(math.pack('100', struct.pack('100', int(2))) bleu_metric: 12.920725243713852 ---------- NL: find all files in directory "/mydir" with extension ".txt" ref: for file in glob.glob('*.txt'): pass Output: results += [each for each in os.listdir('myfile.txt') if each.endswith('.txt'): bleu_metric: 17.995816286711673 ---------- NL: find all files in directory "/mydir" with extension ".txt" ref: for file in os.listdir('/mydir'): if file.endswith('.txt'): pass Output: results += [each for each in os.listdir('myfile.txt') if each.endswith('.txt'): bleu_metric: 46.726782846422246 ---------- NL: find all files in directory "/mydir" with extension ".txt" ref: for (root, dirs, files) in os.walk('/mydir'): for file in files: if file.endswith('.txt'): pass Output: results += [each for each in os.listdir('myfile.txt') if each.endswith('.txt'): bleu_metric: 31.01992438627778 ---------- NL: plot dataframe `df` without a legend ref: df.plot(legend=False) Output: plt.legend(loc=2, fontsize='y') bleu_metric: 13.380161378318956 ---------- NL: loop through the ip address range "192.168.x.x" ref: for i in range(256): for j in range(256): ip = ('192.168.%d.%d' % (i, j)) print(ip) Output: for i in range(0, 10) bleu_metric: 2.18006837039135 ---------- NL: loop through the ip address range "192.168.x.x" ref: for (i, j) in product(list(range(256)), list(range(256))): pass Output: for i in range(0, 10) bleu_metric: 3.8730052084654263 ---------- NL: loop through the ip address range "192.168.x.x" ref: generator = iter_iprange('192.168.1.1', '192.168.255.255', step=1) Output: for i in range(0, 10) bleu_metric: 2.8194161881384923 ---------- NL: sum the corresponding decimal values for binary values of each boolean element in list `x` ref: sum(1 << i for i, b in enumerate(x) if b) Output: sum(map(int, x)) bleu_metric: 10.55672713033355 ---------- NL: write multiple strings `line1`, `line2` and `line3` in one line in a file `target` ref: target.write('%r\n%r\n%r\n' % (line1, line2, line3)) Output: f.write('\n') bleu_metric: 8.523846965585486 ---------- NL: convert list of lists `data` into a flat list ref: [y for x in data for y in (x if isinstance(x, list) else [x])] Output: [item for sublist in l for item in sublist] bleu_metric: 6.735737120156013 ---------- NL: print new line character as `\n` in a string `foo\nbar` ref: print('foo\nbar'.encode('string_escape')) Output: print('\n'.join(parts)) bleu_metric: 23.92121113361141 ---------- NL: remove last comma character ',' in string `s` ref: """""".join(s.rsplit(',', 1)) Output: s = s.replace(',', '') bleu_metric: 24.4545047029932 ---------- NL: calculate the mean of each element in array `x` with the element previous to it ref: (x[1:] + x[:-1]) / 2 Output: x = np.mean(a, axis=1) bleu_metric: 9.241605197022109 ---------- NL: get an array of the mean of each two consecutive values in numpy array `x` ref: x[:-1] + (x[1:] - x[:-1]) / 2 Output: np.mean(np.arange(len(x)) + 3).mean(axis=1)) bleu_metric: 7.6047020452233385 ---------- NL: load data containing `utf-8` from file `new.txt` into numpy array `arr` ref: arr = numpy.fromiter(codecs.open('new.txt', encoding='utf-8'), dtype=' 0} bleu_metric: 45.78553260521078 ---------- NL: get rid of None values in dictionary `kwargs` ref: res = dict((k, v) for k, v in kwargs.items() if v is not None) Output: {i: v for i, v in list(d.items()) if v > 0} bleu_metric: 23.068994361104984 ---------- NL: capture final output of a chain of system commands `ps -ef | grep something | wc -l` ref: subprocess.check_output('ps -ef | grep something | wc -l', shell=True) Output: subprocess.call('echo "foo"', shell=True) bleu_metric: 33.54121358422117 ---------- NL: concatenate a list of strings `['a', 'b', 'c']` ref: """""".join(['a', 'b', 'c']) Output: print(('a', 'b', 'c')) bleu_metric: 42.80768209469581 ---------- NL: find intersection data between series `s1` and series `s2` ref: pd.Series(list(set(s1).intersection(set(s2)))) Output: s1 = pd.DataFrame(s1, s2=None) bleu_metric: 10.722703083793286 ---------- NL: sending http headers to `client` ref: client.send('HTTP/1.0 200 OK\r\n') Output: s.send('HTTP/1.0 200 OK\r\n') bleu_metric: 94.57416090031758 ---------- NL: Format a datetime string `when` to extract date only ref: then = datetime.datetime.strptime(when, '%Y-%m-%d').date() Output: datetime.strptime('2012-11-13 10:33:54.227806', '%Y-%m-%d bleu_metric: 44.26225967370315 ---------- NL: split a multi-line string `inputString` into separate strings ref: inputString.split('\n') Output: a, b, c = input('string_escape') bleu_metric: 21.972813874997158 ---------- NL: Split a multi-line string ` a \n b \r\n c ` by new line character `\n` ref: ' a \n b \r\n c '.split('\n') Output: line.rstrip('\n\n') bleu_metric: 27.97842714002644 ---------- NL: concatenate elements of list `b` by a colon ":" ref: """:""".join(str(x) for x in b) Output: print(repr(a)) bleu_metric: 4.9422115544674705 ---------- NL: get the first object from a queryset in django model `Entry` ref: Entry.objects.filter()[:1].get() Output: Entry.objects.get_values('id') bleu_metric: 23.929967984761056 ---------- NL: Calculate sum over all rows of 2D numpy array ref: a.sum(axis=1) Output: np.sum(a.shape[1:]) bleu_metric: 24.022110864391532 ---------- NL: enable warnings using action 'always' ref: warnings.simplefilter('always') Output: os.kill(logging.DEBUG) bleu_metric: 21.105340631872636 ---------- NL: concatenate items of list `l` with a space ' ' ref: print(' '.join(map(str, l))) Output: print(' '.join(map(str, l)) bleu_metric: 91.69729150999534 ---------- NL: run script 'hello.py' with argument 'htmlfilename.htm' on terminal using python executable ref: subprocess.call(['python.exe', 'hello.py', 'htmlfilename.htm']) Output: subprocess.call('start "$file.py', shell=True) bleu_metric: 24.391589282046496 ---------- NL: How can I parse a time string containing milliseconds in it with python? ref: time.strptime('30/03/09 16:31:32.123', '%d/%m/%y %H:%M:%S.%f') Output: time.strftime('%H:%M:%S,%f')) bleu_metric: 19.792633055621124 ---------- NL: convert a string `my_string` with dot and comma into a float number `my_float` ref: my_float = float(my_string.replace(',', '')) Output: my_string.replace(',', '') bleu_metric: 62.69839476367552 ---------- NL: convert a string `123,456.908` with dot and comma into a floating number ref: float('123,456.908'.replace(',', '')) Output: print(', '.join(str(12345)) bleu_metric: 21.93127055132894 ---------- NL: set pythonpath in python script. ref: sys.path.append('/path/to/whatever') Output: os.path.abspath(__file__) bleu_metric: 14.91608246051739 ---------- NL: split string 'Words, words, words.' using a regex '(\\W+)' ref: re.split('(\\W+)', 'Words, words, words.') Output: re.split('\\b(\\w+)', 'Words, words, words.') bleu_metric: 71.4022021970798 ---------- NL: open a file `Output.txt` in append mode ref: file = open('Output.txt', 'a') Output: f = open('file.txt', 'r') bleu_metric: 54.7332024239071 ---------- NL: download a file "http://www.example.com/songs/mp3.mp3" over HTTP and save to "mp3.mp3" ref: urllib.request.urlretrieve('http://www.example.com/songs/mp3.mp3', 'mp3.mp3') Output: urllib.request.urlretrieve('http://randomsite.com/', 'rb').read() bleu_metric: 45.9127788668896 ---------- NL: download a file `url` over HTTP and save to `file_name` ref: u = urllib.request.urlopen(url) Output: urllib.request.urlretrieve('http://example.com/file.gz') bleu_metric: 19.228544753133757 ---------- NL: download a file 'http://www.example.com/' over HTTP ref: response = urllib.request.urlopen('http://www.example.com/') html = response.read() Output: urllib.request.urlretrieve('http://www.com/file.com/file.gz') bleu_metric: 41.91356463630533 ---------- NL: download a file `url` over HTTP ref: r = requests.get(url) Output: r = requests.get(url) bleu_metric: 100.0 ---------- NL: download a file `url` over HTTP and save to "10MB" ref: response = requests.get(url, stream=True) Output: urllib.request.urlretrieve('http://example.com/file.com/file.gz') bleu_metric: 7.265857755970445 ---------- NL: argparse add argument with flag '--version' and version action of '%(prog)s 2.0' to parser `parser` ref: parser.add_argument('--version', action='version', version='%(prog)s 2.0') Output: parser.add_argument('--conf', nargs='*') bleu_metric: 24.648885368725924 ---------- NL: remove key 'c' from dictionary `d` ref: {i: d[i] for i in d if i != 'c'} Output: d = {'a': 1, 'c': 2, 'c': 3, 'c': 3} bleu_metric: 10.884267615693712 ---------- NL: Create new DataFrame object by merging columns "key" of dataframes `split_df` and `csv_df` and rename the columns from dataframes `split_df` and `csv_df` with suffix `_left` and `_right` respectively ref: pd.merge(split_df, csv_df, on=['key'], suffixes=('_left', '_right')) Output: pd.concat([df_1, df_2.columns], axis=1, on=['A_B', 'B' bleu_metric: 24.624806058504372 ---------- NL: Split a string `s` by space with `4` splits ref: s.split(' ', 4) Output: [s.split('\t')[-1] for s in s.split()] bleu_metric: 19.696330878717074 ---------- NL: read keyboard-input ref: input('Enter your input:') Output: sys.stdout.flush() bleu_metric: 17.6625510283176 ---------- NL: enable debug mode on Flask application `app` ref: app.run(debug=True) Output: app.run(debug=True) bleu_metric: 100.0 ---------- NL: python save list `mylist` to file object 'save.txt' ref: pickle.dump(mylist, open('save.txt', 'wb')) Output: file.write('myfile.txt', 'wb') bleu_metric: 42.60520663282873 ---------- NL: Multiply a matrix `P` with a 3d tensor `T` in scipy ref: scipy.tensordot(P, T, axes=[1, 1]).swapaxes(0, 1) Output: np.dot(x, y) bleu_metric: 4.172184841899331 ---------- NL: Create 3d array of zeroes of size `(3,3,3)` ref: numpy.zeros((3, 3, 3)) Output: np.zeros((3, 3)) bleu_metric: 75.33808072882879 ---------- NL: cut off the last word of a sentence `content` ref: """ """.join(content.split(' ')[:-1]) Output: """ """.join(s.split()) bleu_metric: 46.40451719480771 ---------- NL: convert scalar `x` to array ref: x = np.asarray(x).reshape(1, -1)[(0), :] Output: np.zeros((x, x) for x in s) bleu_metric: 9.777229794850028 ---------- NL: sum all elements of nested list `L` ref: sum(sum(i) if isinstance(i, list) else i for i in L) Output: sum(sum(x) for x in list) bleu_metric: 18.410840441136763 ---------- NL: convert hex string '470FC614' to a float number ref: struct.unpack('!f', '470FC614'.decode('hex'))[0] Output: struct.unpack('d', struct.pack('Q', 16)).decode('hex')) bleu_metric: 43.149269702667276 ---------- NL: Multiple each value by `2` for all keys in a dictionary `my_dict` ref: my_dict.update((x, y * 2) for x, y in list(my_dict.items())) Output: dict((k, v) for k, v in my_dict.items()) bleu_metric: 28.26550613903659 ---------- NL: running bash script 'sleep.sh' ref: subprocess.call('sleep.sh', shell=True) Output: subprocess.call('start command -c 'r', shell=True) bleu_metric: 55.7853798004205 ---------- NL: Join elements of list `l` with a comma `,` ref: """,""".join(l) Output: [(x + x) for x, y in zip(l1, l2)] bleu_metric: 8.039313477786735 ---------- NL: make a comma-separated string from a list `myList` ref: myList = ','.join(map(str, myList)) Output: myList = mylist[:n] bleu_metric: 10.218289380194193 ---------- NL: reverse the list that contains 1 to 10 ref: list(reversed(list(range(10)))) Output: L[::-1] bleu_metric: 7.575149194183216 ---------- NL: remove substring 'bag,' from a string 'lamp, bag, mirror' ref: print('lamp, bag, mirror'.replace('bag,', '')) Output: [m.strip() for m in x if m.endswith('@')] bleu_metric: 10.616735292999413 ---------- NL: Reverse the order of words, delimited by `.`, in string `s` ref: """.""".join(s.split('.')[::-1]) Output: sorted(s, s.split()) bleu_metric: 9.489232285686615 ---------- NL: convert epoch time represented as milliseconds `s` to string using format '%Y-%m-%d %H:%M:%S.%f' ref: datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f') Output: print(s.strftime('%Y-%m-%d %H:%M:%S')) bleu_metric: 62.56418230541474 ---------- NL: parse milliseconds epoch time '1236472051807' to format '%Y-%m-%d %H:%M:%S' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(1236472051807 / 1000.0)) Output: time.strftime('%Y-%m-%d %H:%M:%S') bleu_metric: 59.71758060527691 ---------- NL: get the date 7 days before the current date ref: (datetime.datetime.now() - datetime.timedelta(days=7)).date() Output: (datetime.date(2010, 12, 31) + relativedelta(months=(+ 1))) bleu_metric: 23.713320246552005 ---------- NL: sum elements at index `column` of each list in list `data` ref: print(sum(row[column] for row in data)) Output: data[sum(b) for b in zip(*data)] bleu_metric: 15.844501337268929 ---------- NL: sum columns of a list `array` ref: [sum(row[i] for row in array) for i in range(len(array[0]))] Output: sum(sum(row[:, (1)] for row in mylist) bleu_metric: 21.03312756130989 ---------- NL: encode binary string 'your string' to base64 code ref: base64.b64encode(bytes('your string', 'utf-8')) Output: print(content.encode('base64')) bleu_metric: 20.291424899466545 ---------- NL: combine list of dictionaries `dicts` with the same keys in each list to a single dictionary ref: dict((k, [d[k] for d in dicts]) for k in dicts[0]) Output: dict((k, v) for d in dicts for k, v in list(d.items()) if k in d] bleu_metric: 28.548259613272815 ---------- NL: Merge a nested dictionary `dicts` into a flat dictionary by concatenating nested values with the same key `k` ref: {k: [d[k] for d in dicts] for k in dicts[0]} Output: dict((k, v) for k, v in list(d.items())) bleu_metric: 8.608775825594615 ---------- NL: How do I get the url parameter in a Flask view ref: request.args['myParam'] Output: request.url bleu_metric: 6.856953606192477 ---------- NL: identify duplicate values in list `mylist` ref: [k for k, v in list(Counter(mylist).items()) if v > 1] Output: set(mylist)) bleu_metric: 2.0605095345578897 ---------- NL: Insert directory 'apps' into directory `__file__` ref: sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'apps')) Output: os.chdir(path) bleu_metric: 1.4431184819037772 ---------- NL: modify sys.path for python module `subdir` ref: sys.path.append(os.path.join(os.path.dirname(__file__), 'subdir')) Output: my_path.append('/path/to/my_file.txt') bleu_metric: 10.285502024406256 ---------- NL: Insert a 'None' value into a SQLite3 table. ref: db.execute("INSERT INTO present VALUES('test2', ?, 10)", (None,)) Output: cursor.execute('INSERT INTO table VALUES (%s, %s)', (None,)) bleu_metric: 53.301554843359774 ---------- NL: flatten list `list_of_menuitems` ref: [image for menuitem in list_of_menuitems for image in menuitem] Output: list_of_lists = [list(f) for f in my_list] bleu_metric: 14.949751774990682 ---------- NL: append elements of a set `b` to a list `a` ref: a.extend(b) Output: a.append(b) bleu_metric: 59.46035575013605 ---------- NL: Append elements of a set to a list in Python ref: a.extend(list(b)) Output: a.extend(b) bleu_metric: 46.81294641285289 ---------- NL: write the data of dataframe `df` into text file `np.txt` ref: np.savetxt('c:\\data\\np.txt', df.values, fmt='%d') Output: df.write.txt', header=None, delimiter='\t') bleu_metric: 17.503492126188856 ---------- NL: write content of DataFrame `df` into text file 'c:\\data\\pandas.txt' ref: df.to_csv('c:\\data\\pandas.txt', header=None, index=None, sep=' ', mode='a') Output: df.to_csv('c:\\data\\data\\data\\t.txt', sep='\t') bleu_metric: 46.606880690561546 ---------- NL: Split a string `x` by last occurrence of character `-` ref: print(x.rpartition('-')[0]) Output: x.split('=')[-1].rsplit() bleu_metric: 21.468316165048364 ---------- NL: get the last part of a string before the character '-' ref: print(x.rsplit('-', 1)[0]) Output: print(x.rfind('-')[0]) bleu_metric: 60.109065739327065 ---------- NL: upload file using FTP ref: ftp.storlines('STOR ' + filename, open(filename, 'r')) Output: webbrowser.open('http://example.com/file.gz') bleu_metric: 12.9205540673015 ---------- NL: add one to the hidden web element with id 'XYZ' with selenium python script ref: browser.execute_script("document.getElementById('XYZ').value+='1'") Output: driver.find_element_by_xpath("//li/label/input[contains(..,'polishpottery')]") bleu_metric: 13.25451214611751 ---------- NL: create array containing the maximum value of respective elements of array `[2, 3, 4]` and array `[1, 5, 2]` ref: np.maximum([2, 3, 4], [1, 5, 2]) Output: np.array([1, 2, 3], [2, 3, 4], [3, 4, 5]]) bleu_metric: 42.651255482235264 ---------- NL: print a list `l` and move first 3 elements to the end of the list ref: print(l[3:] + l[:3]) Output: print(l[:5] + l[0]) bleu_metric: 50.02460338549947 ---------- NL: loop over files in directory '.' ref: for fn in os.listdir('.'): if os.path.isfile(fn): pass Output: for os.path.join(path, filename) bleu_metric: 13.004471619759379 ---------- NL: loop over files in directory `source` ref: for (root, dirs, filenames) in os.walk(source): for f in filenames: pass Output: for os.path.join(path, filename) bleu_metric: 7.344980037582256 ---------- NL: create a random list of integers ref: [int(1000 * random.random()) for i in range(10000)] Output: randomList = [random.random() for _ in range(10)] bleu_metric: 34.3414769312466 ---------- NL: Using %f with strftime() in Python to get microseconds ref: datetime.datetime.now().strftime('%H:%M:%S.%f') Output: datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') bleu_metric: 65.15366202853991 ---------- NL: Google App Engine execute GQL query 'SELECT * FROM Schedule WHERE station = $1' with parameter `foo.key()` ref: db.GqlQuery('SELECT * FROM Schedule WHERE station = $1', foo.key()) Output: G.execute('SELECT * FROM foo WHERE bar = %s AND baz = %s', (param1, param2)) bleu_metric: 24.090844358935918 ---------- NL: filter rows in pandas starting with alphabet 'f' using regular expression. ref: df.b.str.contains('^f') Output: df.dropna(lambda x: x.replace('\\[^\\w\\s]', 'A') bleu_metric: 10.727295782787309 ---------- NL: print a 2 dimensional list `tab` as a table with delimiters ref: print('\n'.join('\t'.join(str(col) for col in row) for row in tab)) Output: print('\n'.join(map(str, row)) bleu_metric: 26.894705347686966 ---------- NL: pandas: delete rows in dataframe `df` based on multiple columns values ref: df.set_index(list('BC')).drop(tuples, errors='ignore').reset_index() Output: df.drop(df.index[[1, 3]], axis=1) bleu_metric: 11.446641525222896 ---------- NL: format the variables `self.goals` and `self.penalties` using string formatting ref: """({:d} goals, ${:d})""".format(self.goals, self.penalties) Output: """{0} {1}""".format(name='bar', 'change!') bleu_metric: 28.1073423324667 ---------- NL: format string "({} goals, ${})" with variables `goals` and `penalties` ref: """({} goals, ${})""".format(self.goals, self.penalties) Output: """{} {}""".format(TotalAmount) bleu_metric: 30.199592877917368 ---------- NL: format string "({0.goals} goals, ${0.penalties})" ref: """({0.goals} goals, ${0.penalties})""".format(self) Output: """{0} {1}""".format(name, 20) bleu_metric: 32.37295639418319 ---------- NL: convert list of lists `L` to list of integers ref: [int(''.join(str(d) for d in x)) for x in L] Output: [int(x) for x in L] bleu_metric: 26.91716452870884 ---------- NL: combine elements of each list in list `L` into digits of a single integer ref: [''.join(str(d) for d in x) for x in L] Output: int(''.join(map(str, x))) bleu_metric: 25.049995525110475 ---------- NL: convert a list of lists `L` to list of integers ref: L = [int(''.join([str(y) for y in x])) for x in L] Output: [int(x) for x in L] bleu_metric: 18.058610039563465 ---------- NL: write the elements of list `lines` concatenated by special character '\n' to file `myfile` ref: myfile.write('\n'.join(lines)) Output: with open('myfile.txt', 'w').read().write('\n') bleu_metric: 30.77874158297155 ---------- NL: removing an element from a list based on a predicate 'X' or 'N' ref: [x for x in ['AAT', 'XAC', 'ANT', 'TTA'] if 'X' not in x and 'N' not in x] Output: [x for x in L if x is not None] bleu_metric: 7.979312731505724 ---------- NL: Remove duplicate words from a string `text` using regex ref: text = re.sub('\\b(\\w+)( \\1\\b)+', '\\1', text) Output: re.sub('(?<=[a-z])\\r?\\w+', '\\1', s) bleu_metric: 43.796510807866646 ---------- NL: count non zero values in each column in pandas data frame ref: df.astype(bool).sum(axis=1) Output: df.apply(lambda x: 0).sum() if sum(x) > 1) bleu_metric: 23.20305803246989 ---------- NL: search for string that matches regular expression pattern '(?.*<' in xml string `line` ref: print(re.search('>.*<', line).group(0)) Output: print(re.search('(?<=\\w+)', line)) bleu_metric: 47.71512105691513 ---------- NL: erase all the contents of a file `filename` ref: open(filename, 'w').close() Output: open('filename', 'w').close() bleu_metric: 70.98108717509044 ---------- NL: convert a string into datetime using the format '%Y-%m-%d %H:%M:%S.%f' ref: datetime.datetime.strptime(string_date, '%Y-%m-%d %H:%M:%S.%f') Output: datetime.strptime('2010-11-13 10:33:54.283Z', '%Y-%m-%d bleu_metric: 40.88633662916028 ---------- NL: find the index of a list with the first element equal to '332' within the list of lists `thelist` ref: [index for index, item in enumerate(thelist) if item[0] == '332'] Output: [i for i, v in enumerate(L) if v[0] == 'id' in x] bleu_metric: 33.034466035625115 ---------- NL: lower a string `text` and remove non-alphanumeric characters aside from space ref: re.sub('[^\\sa-zA-Z0-9]', '', text).lower().strip() Output: re.sub('[^\\w-zA-Z0-9]','', text) bleu_metric: 64.45968952353843 ---------- NL: remove all non-alphanumeric characters except space from a string `text` and lower it ref: re.sub('(?!\\s)[\\W_]', '', text).lower().strip() Output: re.sub('[^0-9a-zA-Z0-9]', '', text) bleu_metric: 34.05969875105766 ---------- NL: subscript text 'H20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='H\u2082O') Output: plt.show() bleu_metric: 6.117046115680378 ---------- NL: subscript text 'H20' with '2' as subscripted in matplotlib labels for arrays 'x' and 'y'. ref: plt.plot(x, y, label='$H_2O$') Output: plt.show() bleu_metric: 6.117046115680378 ---------- NL: loop over a list `mylist` if sublists length equals 3 ref: [x for x in mylist if len(x) == 3] Output: [(x + 1) for x in mylist] bleu_metric: 27.97803653491597 ---------- NL: initialize a list `lst` of 100 objects Object() ref: lst = [Object() for _ in range(100)] Output: [item for item in lst] bleu_metric: 11.786767588753086 ---------- NL: create list `lst` containing 100 instances of object `Object` ref: lst = [Object() for i in range(100)] Output: [x for x in lst if x is not None] bleu_metric: 13.386745323687673 ---------- NL: get the content of child tag with`href` attribute whose parent has css `someclass` ref: self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') Output: soup.find_all('a', {'id':'st,'st_name':'st/content': 'content'} bleu_metric: 7.478822222889464 ---------- NL: joining data from dataframe `df1` with data from dataframe `df2` based on matching values of column 'Date_Time' in both dataframes ref: df1.merge(df2, on='Date_Time') Output: s1 = pd.merge(df1, df2, how='inner', on=['user_id']) bleu_metric: 17.17218426146756 ---------- NL: use `%s` operator to print variable values `str1` inside a string ref: 'first string is: %s, second one is: %s' % (str1, 'geo.tif') Output: print('{0}'.format(num)) bleu_metric: 5.68097120736594 ---------- NL: Split a string by a delimiter in python ref: [x.strip() for x in '2.MATCHES $$TEXT$$ STRING'.split('$$TEXT$$')] Output: a.split('=') bleu_metric: 2.904501569386027 ---------- NL: check if directory `directory ` exists and create it if necessary ref: if (not os.path.exists(directory)): os.makedirs(directory) Output: if (not os.path.exists(directory)): os.makedirs(directory) bleu_metric: 100.0 ---------- NL: check if a directory `path` exists and create it if necessary ref: os.makedirs(path) Output: os.makedirs(path) bleu_metric: 100.0 ---------- NL: check if a directory `path` exists and create it if necessary ref: distutils.dir_util.mkpath(path) Output: os.makedirs(path) bleu_metric: 40.661103887968814 ---------- NL: check if a directory `path` exists and create it if necessary ref: os.makedirs(path) Output: os.makedirs(path) bleu_metric: 100.0 ---------- NL: Replace a separate word 'H3' by 'H1' in a string 'text' ref: re.sub('\\bH3\\b', 'H1', text) Output: re.sub('\\b(?= 99) & (df['closing_price'] <= 101)] Output: df[df['column_name'].str[1:] bleu_metric: 9.220008234645237 ---------- NL: replace all occurences of newlines `\n` with `
` in dataframe `df` ref: df.replace({'\n': '
'}, regex=True) Output: df.replace('\n', '>').replace('\r\n') bleu_metric: 28.577933090423212 ---------- NL: replace all occurrences of a string `\n` by string `
` in a pandas data frame `df` ref: df.replace({'\n': '
'}, regex=True) Output: df.replace({'\n': '
'}, regex=True) bleu_metric: 100.0 ---------- NL: create a list containing each two adjacent letters in string `word` as its elements ref: [(x + y) for x, y in zip(word, word[1:])] Output: print(list(itertools.chain.from_iterable([''.join([''.join(i)) for i in myList bleu_metric: 6.9872837779786146 ---------- NL: Get a list of pairs from a string `word` using lambda function ref: list(map(lambda x, y: x + y, word[:-1], word[1:])) Output: [item[0] for item in wordids] bleu_metric: 3.6045344658126863 ---------- NL: extract a url from a string `myString` ref: print(re.findall('(https?://[^\\s]+)', myString)) Output: print(re.search('https://url.com/', mystring)) bleu_metric: 22.139039509717414 ---------- NL: extract a url from a string `myString` ref: print(re.search('(?Phttps?://[^\\s]+)', myString).group('url')) Output: print(re.search('https://url.com/', mystring)) bleu_metric: 19.3178688753789 ---------- NL: remove all special characters, punctuation and spaces from a string `mystring` using regex ref: re.sub('[^A-Za-z0-9]+', '', mystring) Output: re.sub('[^\\w' ]", '', mystring) bleu_metric: 54.67160268053085 ---------- NL: create a DatetimeIndex containing 13 periods of the second friday of each month starting from date '2016-01-01' ref: pd.date_range('2016-01-01', freq='WOM-2FRI', periods=13) Output: df.date_of_datetime(df.strftime('%Y-%m-%d') bleu_metric: 9.119026367194746 ---------- NL: Create multidimensional array `matrix` with 3 rows and 2 columns in python ref: matrix = [[a, b], [c, d], [e, f]] Output: np.array([[0, 0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 0 bleu_metric: 11.802861352393501 ---------- NL: replace spaces with underscore ref: mystring.replace(' ', '_') Output: re.sub('\\([^\\w\\)', '', text) bleu_metric: 15.604242268653643 ---------- NL: get an absolute file path of file 'mydir/myfile.txt' ref: os.path.abspath('mydir/myfile.txt') Output: os.path.abspath(mypath) bleu_metric: 36.304072644520666 ---------- NL: split string `my_string` on white spaces ref: """ """.join(my_string.split()) Output: my_string.split('_') bleu_metric: 19.77851893672361 ---------- NL: get filename without extension from file `filename` ref: os.path.splitext(filename)[0] Output: os.path.splitext('/path/to/filename.txt') bleu_metric: 33.70129264673147 ---------- NL: get a list containing the sum of each element `i` in list `l` plus the previous elements ref: [sum(l[:i]) for i, _ in enumerate(l)] Output: [sum(x) for x in lst if x > 2] bleu_metric: 20.61396573920308 ---------- NL: split a string `Docs/src/Scripts/temp` by `/` keeping `/` in the result ref: """Docs/src/Scripts/temp""".replace('/', '/\x00/').split('\x00') Output: s.split('\t')) bleu_metric: 4.851225711700038 ---------- NL: shuffle columns of an numpy array 'r' ref: np.random.shuffle(np.transpose(r)) Output: np.random.shuffle(np.random.randint(0, 1, axis=1)) bleu_metric: 41.3948387915005 ---------- NL: copy all values in a column 'B' to a new column 'D' in a pandas data frame 'df' ref: df['D'] = df['B'] Output: df['B'] = df['B'] bleu_metric: 80.03203203844998 ---------- NL: find a value within nested json 'data' where the key inside another key 'B' is unknown. ref: list(data['A']['B'].values())[0]['maindata'][0]['Info'] Output: data[(key=lambda x: x['Date']) & (x['Date']['count'])] bleu_metric: 17.180939455924943 ---------- NL: check characters of string `string` are true predication of function `predicate` ref: all(predicate(x) for x in string) Output: all(x in string for x in list(string.items()) if x == 'isdit') bleu_metric: 16.27934873162478 ---------- NL: determine number of files on a drive with python ref: os.statvfs('/').f_files - os.statvfs('/').f_ffree Output: os.stat(path).st_common(f.st_f) bleu_metric: 10.276795791404941 ---------- NL: how to get a single result from a SQLite query in python? ref: cursor.fetchone()[0] Output: cursor.execute('SELECT * FROM foo WHERE bar = %s' % (f, data)) bleu_metric: 8.282282660969601 ---------- NL: convert string `user_input` into a list of integers `user_list` ref: user_list = [int(number) for number in user_input.split(',')] Output: return int(soup.split(',')) bleu_metric: 34.34281712615472 ---------- NL: Get a list of integers by splitting a string `user` with comma ref: [int(s) for s in user.split(',')] Output: [int(x) for x in (int(x)] for x in s.split('')] bleu_metric: 32.12854967972961 ---------- NL: Sorting a Python list by two criteria ref: sorted(list, key=lambda x: (x[0], -x[1])) Output: sorted(list, key=lambda x: (x[1], x[0])) bleu_metric: 80.44410811781832 ---------- NL: sort a list of objects `ut`, based on a function `cmpfun` in descending order ref: ut.sort(key=cmpfun, reverse=True) Output: sorted(a, key=lambda x: x.count(x.getter(1)) bleu_metric: 10.813005337959174 ---------- NL: reverse list `ut` based on the `count` attribute of each object ref: ut.sort(key=lambda x: x.count, reverse=True) Output: sum(lst) bleu_metric: 2.867683507302 ---------- NL: sort a list of objects `ut` in reverse order by their `count` property ref: ut.sort(key=lambda x: x.count, reverse=True) Output: sorted(L, key=lambda x: (-len(x.split('-))) bleu_metric: 25.0737833894674 ---------- NL: click a href button 'Send' with selenium ref: driver.find_element_by_partial_link_text('Send').click() Output: browser.find_element_by_text('http://www.text.com') bleu_metric: 14.44788670919441 ---------- NL: click a href button having text `Send InMail` with selenium ref: driver.findElement(By.linkText('Send InMail')).click() Output: browser.find_element_by_class_name('http://www.text.com') bleu_metric: 11.088142201985505 ---------- NL: click a href button with text 'Send InMail' with selenium ref: driver.find_element_by_link_text('Send InMail').click() Output: browser.find_element_by_text('http://www.text.com') bleu_metric: 14.44788670919441 ---------- NL: cast an int `i` to a string and concat to string 'ME' ref: 'ME' + str(i) Output: """""".join(i) bleu_metric: 31.61487584488943 ---------- NL: Sorting data in DataFrame Pandas ref: df.sort_values(['System_num', 'Dis']) Output: df.sort_values(['System_num', 'Dis']) bleu_metric: 100.0 ---------- NL: prepend the line '#test firstline\n' to the contents of file 'infile' and save as the file 'outfile' ref: open('outfile', 'w').write('#test firstline\n' + open('infile').read()) Output: open('filename', 'w').write('\n').read().write('\n').write('\n') bleu_metric: 45.445331007629186 ---------- NL: sort a list `l` by length of value in tuple ref: l.sort(key=lambda t: len(t[1]), reverse=True) Output: print(sorted(lst, key=lambda tup: len(x), len(y))) bleu_metric: 17.729842264695016 ---------- NL: split string `s` by words that ends with 'd' ref: re.findall('\\b(\\w+)d\\b', s) Output: [word for word in s.split('.')[0] for s in s.split()] bleu_metric: 7.987276352377326 ---------- NL: return `True` if string `foobarrrr` contains regex `ba[rzd]` ref: bool(re.search('ba[rzd]', 'foobarrrr')) Output: re.findall('(?<=\\d+', 'aaa') bleu_metric: 18.49304691034943 ---------- NL: Removing duplicates in list `t` ref: list(set(t)) Output: list(set(item) for item in L)) bleu_metric: 35.556702356686955 ---------- NL: Removing duplicates in list `source_list` ref: list(set(source_list)) Output: list(item) bleu_metric: 24.795364698947967 ---------- NL: Removing duplicates in list `abracadabra` ref: list(OrderedDict.fromkeys('abracadabra')) Output: list(set(t)) bleu_metric: 19.745471508497758 ---------- NL: Convert array `a` into a list ref: numpy.array(a).reshape(-1).tolist() Output: a.append(a) bleu_metric: 9.852859740626553 ---------- NL: Convert the first row of numpy matrix `a` to a list ref: numpy.array(a)[0].tolist() Output: numpy.array(a, a[0]) bleu_metric: 44.579146390257485 ---------- NL: In `soup`, get the content of the sibling of the `td` tag with text content `Address:` ref: print(soup.find(text='Address:').findNext('td').contents[0]) Output: soup.findAll(text='Python') bleu_metric: 11.86327327630691 ---------- NL: convert elements of each tuple in list `l` into a string separated by character `@` ref: """ """.join([('%d@%d' % t) for t in l]) Output: """""".join(map(lambda x: x[1]) bleu_metric: 37.15744930678825 ---------- NL: convert each tuple in list `l` to a string with '@' separating the tuples' elements ref: """ """.join([('%d@%d' % (t[0], t[1])) for t in l]) Output: [(x + y) for x, y in zip(l1, l2)] bleu_metric: 4.866055054993489 ---------- NL: get the html from the current web page of a Selenium driver ref: driver.execute_script('return document.documentElement.outerHTML;') Output: driver.get('http://www.google.com') bleu_metric: 19.209534151258666 ---------- NL: Get all matches with regex pattern `\\d+[xX]` in list of string `teststr` ref: [i for i in teststr if re.search('\\d+[xX]', i)] Output: [m.start() for m in re.findall('\\d+', 'abc test test test)] bleu_metric: 29.15471806136295 ---------- NL: select values from column 'A' for which corresponding values in column 'B' will be greater than 50, and in column 'C' - equal 900 in dataframe `df` ref: df['A'][(df['B'] > 50) & (df['C'] == 900)] Output: df[['A', 'B']].fillna(df['A'] bleu_metric: 32.2826123723905 ---------- NL: Sort dictionary `o` in ascending order based on its keys and items ref: sorted(o.items()) Output: sorted(list(d.items()), key=lambda x: x[1]) bleu_metric: 24.42195895839763 ---------- NL: get sorted list of keys of dict `d` ref: sorted(d) Output: sorted(d, key=d.get) bleu_metric: 25.21193618434983 ---------- NL: How to sort dictionaries by keys in Python ref: sorted(d.items()) Output: sorted(d.items()), key=lambda x: (x['name'], x['name'], x['name'])) bleu_metric: 21.62882016096312 ---------- NL: convert string "1" into integer ref: int('1') Output: int('1', 2) bleu_metric: 58.143073696821936 ---------- NL: function to convert strings into integers ref: int() Output: int(''.join(map(str, x))) bleu_metric: 12.729922658368398 ---------- NL: convert items in `T1` to integers ref: T2 = [map(int, x) for x in T1] Output: int('1') bleu_metric: 7.448668213629092 ---------- NL: call a shell script `./test.sh` using subprocess ref: subprocess.call(['./test.sh']) Output: subprocess.call('test.sh otherfunc') bleu_metric: 34.64490516910983 ---------- NL: call a shell script `notepad` using subprocess ref: subprocess.call(['notepad']) Output: subprocess.call(['notepad']) bleu_metric: 100.0 ---------- NL: combine lists `l1` and `l2` by alternating their elements ref: [val for pair in zip(l1, l2) for val in pair] Output: [(x + y) for x, y in zip(l1, l2)] bleu_metric: 40.59264050928986 ---------- NL: encode string 'data to be encoded' ref: encoded = base64.b64encode('data to be encoded') Output: data = json.encode('utf-8') bleu_metric: 21.665290989148364 ---------- NL: encode a string `data to be encoded` to `ascii` encoding ref: encoded = 'data to be encoded'.encode('ascii') Output: data = 'data2' bleu_metric: 6.5298221612721505 ---------- NL: parse tab-delimited CSV file 'text.txt' into a list ref: lol = list(csv.reader(open('text.txt', 'rb'), delimiter='\t')) Output: csv.writerow(open('filename.txt', 'r').read() bleu_metric: 26.04837362077714 ---------- NL: Get attribute `my_str` of object `my_object` ref: getattr(my_object, my_str) Output: my_obj.get('id') bleu_metric: 19.96019880774733 ---------- NL: group a list of dicts `LD` into one dict by key ref: print(dict(zip(LD[0], zip(*[list(d.values()) for d in LD])))) Output: dict(zip(d.items()) for d in L)} bleu_metric: 21.362647244490635 ---------- NL: How do I sum the first value in each tuple in a list of tuples in Python? ref: sum([pair[0] for pair in list_of_pairs]) Output: sum(item[0] for item in mylist) bleu_metric: 32.21542689451684 ---------- NL: convert unicode string u"{'code1':1,'code2':1}" into dictionary ref: d = ast.literal_eval("{'code1':1,'code2':1}") Output: {'MyString1': '1', '1'}).encode('ascii', 'ignore') bleu_metric: 14.294565332608489 ---------- NL: find all words in a string `mystring` that start with the `$` sign ref: [word for word in mystring.split() if word.startswith('$')] Output: re.findall('\\[^\\w\\w', mystring) bleu_metric: 9.573479881751316 ---------- NL: remove any url within string `text` ref: text = re.sub('^https?:\\/\\/.*[\\r\\n]*', '', text, flags=re.MULTILINE) Output: text = re.sub('\\r\\r\\r\\n*\\Z', '', text) bleu_metric: 39.07206775645457 ---------- NL: replace all elements in array `A` that are not present in array `[1, 3, 4]` with zeros ref: np.where(np.in1d(A, [1, 3, 4]).reshape(A.shape), A, 0) Output: np.isnan(a).any(np.in1d(a), 0, a) bleu_metric: 20.39361671090609 ---------- NL: calculate mean across dimension in a 2d array `a` ref: np.mean(a, axis=1) Output: np.mean(a, axis=1) bleu_metric: 100.0 ---------- NL: running r script '/pathto/MyrScript.r' from python ref: subprocess.call(['/usr/bin/Rscript', '--vanilla', '/pathto/MyrScript.r']) Output: subprocess.call('path/to/test.py', shell=True) bleu_metric: 12.706804596574397 ---------- NL: run r script '/usr/bin/Rscript --vanilla /pathto/MyrScript.r' ref: subprocess.call('/usr/bin/Rscript --vanilla /pathto/MyrScript.r', shell=True) Output: subprocess.call('/usr/bin/perl', shell=True) bleu_metric: 48.72501457287311 ---------- NL: add a header to a csv file ref: writer.writeheader() Output: writer.writeheader() bleu_metric: 100.0 ---------- NL: replacing nan in the dataframe `df` with row average ref: df.fillna(df.mean(axis=1), axis=1) Output: df.fillna(df.mean(axis=1), axis=0) bleu_metric: 86.6397551781362 ---------- NL: Convert unix timestamp '1347517370' to formatted string '%Y-%m-%d %H:%M:%S' ref: time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370)) Output: datetime.strptime('21/11/13 19:19:30 -0400', '%Y/%m/%Y bleu_metric: 11.771247290341437 ---------- NL: Call a base class's class method `do` from derived class `Derived` ref: super(Derived, cls).do(a) Output: super(MyClass, self).__init__(*args, **kwargs) bleu_metric: 14.02577516008147 ---------- NL: selecting rows in Numpy ndarray 'a', where the value in the first column is 0 and value in the second column is 1 ref: a[np.where((a[:, (0)] == 0) * (a[:, (1)] == 1))] Output: a[np.arange(a[0])!= 0] bleu_metric: 10.315793886636692 ---------- NL: separate words delimited by one or more spaces into a list ref: re.split(' +', 'hello world sample text') Output: [word for word in my_list if any(x) not in '\n' % x.split()] bleu_metric: 10.332090908268508 ---------- NL: length of longest element in list `words` ref: len(max(words, key=len)) Output: len(words, key=lambda i: len(word) for word in word_list) bleu_metric: 30.51327880362441 ---------- NL: get the value associated with unicode key 'from_user' of first dictionary in list `result` ref: result[0]['from_user'] Output: [x for x in list_of_dict if x['success'] not in data] bleu_metric: 12.879862858915844 ---------- NL: Retrieve each line from a file 'File.txt' as a list ref: [line.split() for line in open('File.txt')] Output: line = [line.strip() for line in open('filename.txt')] bleu_metric: 63.83964846132485 ---------- NL: swap keys with values in a dictionary `a` ref: res = dict((v, k) for k, v in a.items()) Output: dict((v, k) for k, v in a.items()) bleu_metric: 90.00876262522594 ---------- NL: Open a file `path/to/FILE_NAME.ext` in write mode ref: new_file = open('path/to/FILE_NAME.ext', 'w') Output: shutil.copyfile(path, '/to/file.txt') bleu_metric: 20.55825140665624 ---------- NL: How to count distinct values in a column of a pandas group by object? ref: df.groupby(['col1', 'col2'])['col3'].nunique().reset_index() Output: df.groupby('A').agg(lambda x: x.nlargest(x.name).nunique()) bleu_metric: 24.81076483975817 ---------- NL: Check if any key in the dictionary `dict1` starts with the string `EMP$$` ref: any(key.startswith('EMP$$') for key in dict1) Output: any(dict.values()) bleu_metric: 9.663861439684919 ---------- NL: create list of values from dictionary `dict1` that have a key that starts with 'EMP$$' ref: [value for key, value in list(dict1.items()) if key.startswith('EMP$$')] Output: [key for key, value in list(d.items()) if 'new y' in d] bleu_metric: 43.11804919179004 ---------- NL: convert a pandas series `sf` into a pandas dataframe `df` with columns `email` and `list` ref: pd.DataFrame({'email': sf.index, 'list': sf.values}) Output: pd.concat([df1, df2], axis=1) bleu_metric: 8.12657367724027 ---------- NL: print elements of list `list` seperated by tabs `\t` ref: print('\t'.join(map(str, list))) Output: print(' '.join(map(list, list))) bleu_metric: 62.705622531832795 ---------- NL: print unicode string '\xd0\xbf\xd1\x80\xd0\xb8' with utf-8 ref: print('\xd0\xbf\xd1\x80\xd0\xb8'.encode('raw_unicode_escape')) Output: print('\xd0\xd0\xd1\xd0\xd0\xd0\xd0\xd0\xd0\ bleu_metric: 32.278364081487986 ---------- NL: Encode a latin character in string `Sopet\xc3\xb3n` properly ref: 'Sopet\xc3\xb3n'.encode('latin-1').decode('utf-8') Output: print('\xc3\xa9'.encode('utf-8')) bleu_metric: 40.96260380345688 ---------- NL: resized image `image` to width, height of `(x, y)` with filter of `ANTIALIAS` ref: image = image.resize((x, y), Image.ANTIALIAS) Output: cv2.imshow(image, 0, cv2.COLOR_BGR2RGB) bleu_metric: 12.162636437767492 ---------- NL: regex, find "n"s only in the middle of string `s` ref: re.findall('n(?<=[^n]n)n+(?=[^n])(?i)', s) Output: re.sub('(?<=n)', '', s) bleu_metric: 19.97638457636558 ---------- NL: display the float `1/3*100` as a percentage ref: print('{0:.0f}%'.format(1.0 / 3 * 100)) Output: int(round(1.5)) bleu_metric: 8.651705771167139 ---------- NL: sort a list of dictionary `mylist` by the key `title` ref: mylist.sort(key=lambda x: x['title']) Output: mylist.sort(key=lambda x: x['title']) bleu_metric: 100.0 ---------- NL: sort a list `l` of dicts by dict value 'title' ref: l.sort(key=lambda x: x['title']) Output: l.sort(key=lambda x: x['title']) bleu_metric: 100.0 ---------- NL: sort a list of dictionaries by the value of keys 'title', 'title_url', 'id' in ascending order. ref: l.sort(key=lambda x: (x['title'], x['title_url'], x['id'])) Output: my_list.sort(key=lambda x: x['title']) bleu_metric: 30.53233854712605 ---------- NL: find 10 largest differences between each respective elements of list `l1` and list `l2` ref: heapq.nlargest(10, range(len(l1)), key=lambda i: abs(l1[i] - l2[i])) Output: print(max(x, key=sum)) bleu_metric: 5.1753387054451085 ---------- NL: BeautifulSoup find all 'span' elements in HTML string `soup` with class of 'starGryB sp' ref: soup.find_all('span', {'class': 'starGryB sp'}) Output: soup.findAll('div', attrs={'name': 'description'}) bleu_metric: 27.858167198909346 ---------- NL: write records in dataframe `df` to table 'test' in schema 'a_schema' ref: df.to_sql('test', engine, schema='a_schema') Output: df.to_csv('test.csv', index=False) bleu_metric: 20.519505687240557 ---------- NL: Extract brackets from string `s` ref: brackets = re.sub('[^(){}[\\]]', '', s) Output: re.findall('\\[\\w\\]', s) bleu_metric: 28.25241115931726 ---------- NL: remove duplicate elements from list 'L' ref: list(dict((x[0], x) for x in L).values()) Output: [set(x) for x in set(tuple(x) for x in L)] bleu_metric: 35.03419928300855 ---------- NL: read a file `file` without newlines ref: [line.rstrip('\n') for line in file] Output: f.close() bleu_metric: 4.4408502586392995 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 9.025970847670083 ---------- NL: get the position of item 1 in `testlist` ref: [i for (i, x) in enumerate(testlist) if (x == 1)] Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 9.025970847670083 ---------- NL: get the position of item 1 in `testlist` ref: for i in [i for (i, x) in enumerate(testlist) if (x == 1)]: pass Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 7.3690983226275435 ---------- NL: get the position of item 1 in `testlist` ref: for i in (i for (i, x) in enumerate(testlist) if (x == 1)): pass Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 8.372912227572577 ---------- NL: get the position of item 1 in `testlist` ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) Output: print(list(range(1, len(mylist)) + 1) bleu_metric: 10.364607550945996 ---------- NL: get the position of item `element` in list `testlist` ref: print(testlist.index(element)) Output: print(testlist.index(x)) bleu_metric: 70.16879391277371 ---------- NL: get the position of item `element` in list `testlist` ref: gen = (i for (i, x) in enumerate(testlist) if (x == 1)) Output: print(testlist.index(x)) bleu_metric: 8.18227813664567 ---------- NL: find the first element of the tuple with the maximum second element in a list of tuples `lis` ref: max(lis, key=lambda item: item[1])[0] Output: max(x[1] for x in L[0]) bleu_metric: 22.078697088920286 ---------- NL: get the item at index 0 from the tuple that has maximum value at index 1 in list `lis` ref: max(lis, key=itemgetter(1))[0] Output: max(a, key=lambda x: x[1]) bleu_metric: 21.186050864016664 ---------- NL: Make a delay of 1 second ref: time.sleep(1) Output: time.sleep(5) bleu_metric: 59.46035575013605 ---------- NL: convert list of tuples `L` to a string ref: """, """.join('(' + ', '.join(i) + ')' for i in L) Output: """""".join(map(str, L)) bleu_metric: 22.382912241881993 ---------- NL: Django set default value of field `b` equal to '0000000' ref: b = models.CharField(max_length=7, default='0000000', editable=False) Output: super(Model, self).save(*args, **kwargs) bleu_metric: 8.756189878973519 ---------- NL: Sort lis `list5` in ascending order based on the degrees value of its elements ref: sorted(list5, lambda x: (degree(x), x)) Output: sorted(list(range(len(a))), key=lambda x: (-len(x)), x[1])) bleu_metric: 21.691223219717052 ---------- NL: How do I perform secondary sorting in python? ref: sorted(list5, key=lambda vertex: (degree(vertex), vertex)) Output: sorted(list1, key=lambda x: x[:-1]) bleu_metric: 23.523340210589698 ---------- NL: convert a list into a generator object ref: (n for n in [1, 2, 3, 5]) Output: [(i * i for i in range(10)) for i in range(10)] bleu_metric: 7.707532488755381 ---------- NL: remove elements from list `oldlist` that have an index number mentioned in list `removelist` ref: newlist = [v for i, v in enumerate(oldlist) if i not in removelist] Output: [item for item in my_list if some_condition()] bleu_metric: 9.65619264674583 ---------- NL: Open a file `yourfile.txt` in write mode ref: f = open('yourfile.txt', 'w') Output: f = open('file.txt', 'rU') bleu_metric: 57.3057404379869 ---------- NL: get attribute 'attr' from object `obj` ref: getattr(obj, 'attr') Output: getattr(obj, 'attr') bleu_metric: 100.0 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to tuple ref: from functools import reduce reduce(lambda a, b: a + b, (('aa',), ('bb',), ('cc',))) Output: tuple(map(tuple, map(int, t)) bleu_metric: 3.3785986651715976 ---------- NL: convert tuple of tuples `(('aa',), ('bb',), ('cc',))` to list in one line ref: map(lambda a: a[0], (('aa',), ('bb',), ('cc',))) Output: [('a', 'b', 'c'), ('d', 'e', 'e']) for e in l] bleu_metric: 15.687545978719285 ---------- NL: Python Pandas: How to replace a characters in a column of a dataframe? ref: df['range'].replace(',', '-', inplace=True) Output: df.replace('-', inplace=True) bleu_metric: 43.27858737448645 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip(*a) bleu_metric: 0.24532394983186231 ---------- NL: unzip the list `[('a', 1), ('b', 2), ('c', 3), ('d', 4)]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4)]) Output: zip(*a) bleu_metric: 0.24532394983186231 ---------- NL: unzip list `original` ref: result = ([a for (a, b) in original], [b for (a, b) in original]) Output: zip(*main_list) bleu_metric: 0.7985269443272172 ---------- NL: unzip list `original` and return a generator ref: result = ((a for (a, b) in original), (b for (a, b) in original)) Output: [(i, j) for i in range(10)] bleu_metric: 5.292278815391249 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` ref: zip(*[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip(*a) bleu_metric: 0.07639466888110501 ---------- NL: unzip list `[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', )]` and fill empty results with None ref: map(None, *[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e',)]) Output: zip(*a) bleu_metric: 0.037243160645090284 ---------- NL: encode `Decimal('3.9')` to a JSON string ref: json.dumps(Decimal('3.9')) Output: json.dumps(str(x)) bleu_metric: 32.39454232489392 ---------- NL: Add key "mynewkey" to dictionary `d` with value "mynewvalue" ref: d['mynewkey'] = 'mynewvalue' Output: d['mykey'] = {'mykey': 1}, {'section': 3}) bleu_metric: 14.908960803395837 ---------- NL: Add key 'a' to dictionary `data` with value 1 ref: data.update({'a': 1, }) Output: data['a'] = 'b' bleu_metric: 17.86189637183548 ---------- NL: Add key 'a' to dictionary `data` with value 1 ref: data.update(dict(a=1)) Output: data['a'] = 'b' bleu_metric: 13.078474031415348 ---------- NL: Add key 'a' to dictionary `data` with value 1 ref: data.update(a=1) Output: data['a'] = 'b' bleu_metric: 14.323145079400494 ---------- NL: find maximal value in matrix `matrix` ref: max([max(i) for i in matrix]) Output: max(max(x, key=lambda x: x[1])[0] bleu_metric: 12.58220555845937 ---------- NL: Round number `answer` to 2 precision after the decimal point ref: answer = str(round(answer, 2)) Output: round(1.005, 2) bleu_metric: 24.5981275183433 ---------- NL: extract ip address from an html string ref: ip = re.findall('[0-9]+(?:\\.[0-9]+){3}', s) Output: print(re.findall('\\d+(?=(\\.com+)\\.com/', s)) bleu_metric: 29.033332017734637 ---------- NL: filter dataframe `df` by values in column `A` that appear more than once ref: df.groupby('A').filter(lambda x: len(x) > 1) Output: df[df['A'] > 1].filter(lambda x: x.isnull().values())] bleu_metric: 29.586757494947246 ---------- NL: append each line in file `myfile` into a list ref: [x for x in myfile.splitlines() if x != ''] Output: myfile.write('myfile.txt', 'w').write('\n') bleu_metric: 8.899891914341412 ---------- NL: Get a list of integers `lst` from a file `filename.txt` ref: lst = map(int, open('filename.txt').readlines()) Output: [int(i) for i in f.split(',')] bleu_metric: 12.313871716496207 ---------- NL: add color bar with image `mappable` to plot `plt` ref: plt.colorbar(mappable=mappable, cax=ax3) Output: plt.savefig('test.png') bleu_metric: 16.449450056138957 ---------- NL: count most frequent 100 words in column 'text' of dataframe `df` ref: Counter(' '.join(df['text']).split()).most_common(100) Output: df.join(word).most_common(word) bleu_metric: 14.732626148274333 ---------- NL: Python split a string using regex ref: re.findall('(.+?):(.+?)\\b ?', text) Output: re.split('(?<=\\.\\d+|\\s)', text) bleu_metric: 27.16899549919204 ---------- NL: generate all 2-element subsets of tuple `(1, 2, 3)` ref: list(itertools.combinations((1, 2, 3), 2)) Output: [list(itertools.product([1, 2, 3, 4], [4, 5, 6])) for a in range( bleu_metric: 23.605722704787006 ---------- NL: get a value of datetime.today() in the UTC time zone ref: datetime.now(pytz.utc) Output: datetime.datetime.utcnow().date() bleu_metric: 18.70361278311548 ---------- NL: Get a new list `list2`by removing empty list from a list of lists `list1` ref: list2 = [x for x in list1 if x != []] Output: list2 = [x for x in list1 if x!= []] bleu_metric: 100.0 ---------- NL: Create `list2` to contain the lists from list `list1` excluding the empty lists from `list1` ref: list2 = [x for x in list1 if x] Output: list23 = [(a - b) for a, b in zip(list1, list2)] bleu_metric: 11.13613656101029 ---------- NL: Django response with JSON `data` ref: return HttpResponse(data, mimetype='application/json') Output: json.dumps(data, ensure_data) bleu_metric: 16.66900658055424 ---------- NL: get all text that is not enclosed within square brackets in string `example_str` ref: re.findall('(.*?)\\[.*?\\]', example_str) Output: """\\[\\]]""".split() bleu_metric: 16.91906599442455 ---------- NL: Use a regex to get all text in a string `example_str` that is not surrounded by square brackets ref: re.findall('(.*?)(?:\\[.*?\\]|$)', example_str) Output: re.findall('\\[(.*?\\]\\]', '', str)) bleu_metric: 41.184938137508176 ---------- NL: get whatever is between parentheses as a single match, and any char outside as an individual match in string '(zyx)bc' ref: re.findall('\\(.+?\\)|\\w', '(zyx)bc') Output: re.findall('\\w+(?:\\w+)', '\\1', s) bleu_metric: 38.05918723545142 ---------- NL: match regex '\\((.*?)\\)|(\\w)' with string '(zyx)bc' ref: re.findall('\\((.*?)\\)|(\\w)', '(zyx)bc') Output: re.match('(?<=\\w+)', 'a\n b\n c') bleu_metric: 21.2203664532429 ---------- NL: match multiple regex patterns with the alternation operator `|` in a string `(zyx)bc` ref: re.findall('\\(.*?\\)|\\w', '(zyx)bc') Output: re.findall('(?<=\\w)\\w', 'abcdefg123 and again abc') bleu_metric: 40.304968802021214 ---------- NL: formate each string cin list `elements` into pattern '%{0}%' ref: elements = ['%{0}%'.format(element) for element in elements] Output: print(' '.join(map(str, list))) bleu_metric: 9.154148084605266 ---------- NL: Open a background process 'background-process' with arguments 'arguments' ref: subprocess.Popen(['background-process', 'arguments']) Output: subprocess.Popen(['file', '-r', '-r', '-r', 'file.file']) bleu_metric: 28.631840575309536 ---------- NL: get list of values from dictionary 'mydict' w.r.t. list of keys 'mykeys' ref: [mydict[x] for x in mykeys] Output: [k for d in mylist if'mykey' in d] bleu_metric: 14.807582680058125 ---------- NL: convert list `[('Name', 'Joe'), ('Age', 22)]` into a dictionary ref: dict([('Name', 'Joe'), ('Age', 22)]) Output: dict(map(int, ['ip', 'at', '7', 'D']) bleu_metric: 17.760449184092803 ---------- NL: average each two columns of array `data` ref: data.reshape(-1, j).mean(axis=1).reshape(data.shape[0], -1) Output: data.sum(axis=1).sum(axis=1) bleu_metric: 19.628836362845185 ---------- NL: double backslash escape all double quotes in string `s` ref: print(s.encode('unicode-escape').replace('"', '\\"')) Output: result = result.replace('\\', '') bleu_metric: 23.514174149586935 ---------- NL: split a string into a list of words and whitespace ref: re.split('(\\W+)', s) Output: [word for word in mystring.split()] bleu_metric: 15.620412264187214 ---------- NL: plotting stacked barplots on a panda data frame ref: df.plot(kind='barh', stacked=True) Output: df.plot(kind='bar', stacked=True) bleu_metric: 81.53551038173114 ---------- NL: reverse the keys and values in a dictionary `myDictionary` ref: {i[1]: i[0] for i in list(myDictionary.items())} Output: dict((v, k) for k, v in my_dict.items()) bleu_metric: 19.54411146921455 ---------- NL: finding the index of elements containing substring 'how' and 'what' in a list of strings 'myList'. ref: [i for i, j in enumerate(myList) if 'how' in j.lower() or 'what' in j.lower()] Output: [x for x in mylist if 'abc' in x] bleu_metric: 4.965473926212578 ---------- NL: check if object `obj` is a string ref: isinstance(obj, str) Output: isinstance(obj, str) bleu_metric: 100.0 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance(a, str) bleu_metric: 59.46035575013605 ---------- NL: check if object `o` is a string ref: (type(o) is str) Output: isinstance(a, str) bleu_metric: 23.813694985189848 ---------- NL: check if object `o` is a string ref: isinstance(o, str) Output: isinstance(a, str) bleu_metric: 59.46035575013605 ---------- NL: check if `obj_to_test` is a string ref: isinstance(obj_to_test, str) Output: if (not some_string): pass bleu_metric: 20.556680845025983 ---------- NL: append list `list1` to `list2` ref: list2.extend(list1) Output: list2.append(list2) bleu_metric: 32.17294420803808 ---------- NL: append list `mylog` to `list1` ref: list1.extend(mylog) Output: mylist.append('myD') bleu_metric: 18.850319022747346 ---------- NL: append list `a` to `c` ref: c.extend(a) Output: a.append(b) bleu_metric: 29.071536848410968 ---------- NL: append items in list `mylog` to `list1` ref: for line in mylog: list1.append(line) Output: mylist.append('myD') bleu_metric: 22.84389301518129 ---------- NL: append a tuple of elements from list `a` with indexes '[0][0] [0][2]' to list `b` ref: b.append((a[0][0], a[0][2])) Output: [(a, b, c) for a, b in zip(a, b)] bleu_metric: 9.850589025894903 ---------- NL: Initialize `SECRET_KEY` in flask config with `Your_secret_string ` ref: app.config['SECRET_KEY'] = 'Your_secret_string' Output: app.config['your_key'] ='my_key_key' bleu_metric: 57.31224484094262 ---------- NL: unpack a series of tuples in pandas into a DataFrame with column names 'out-1' and 'out-2' ref: pd.DataFrame(out.tolist(), columns=['out-1', 'out-2'], index=out.index) Output: pd.concat([df1, df2], axis=1) bleu_metric: 5.130188327095793 ---------- NL: find the index of an element 'MSFT' in a list `stocks_list` ref: [x for x in range(len(stocks_list)) if stocks_list[x] == 'MSFT'] Output: sorted(list_to_index, key=lambda x: x[0].index(x_list) bleu_metric: 7.022279615553664 ---------- NL: rotate the xtick labels of matplotlib plot `ax` by `45` degrees to make long labels readable ref: ax.set_xticklabels(labels, rotation=45) Output: ax.set_xlabel('Temperature (\u2103)') bleu_metric: 16.54461999338998 ---------- NL: remove symbols from a string `s` ref: re.sub('[^\\w]', ' ', s) Output: re.sub('[^\\w]', '', s) bleu_metric: 100.0 ---------- NL: Get the current directory of a script ref: os.path.basename(os.path.dirname(os.path.realpath(__file__))) Output: os.path.dirname(os.path.abspath(__file__)) bleu_metric: 52.085526428001614 ---------- NL: Find octal characters matches from a string `str` using regex ref: print(re.findall("'\\\\[0-7]{1,3}'", str)) Output: re.findall('[^\\w-\\d]', str) bleu_metric: 27.1936649292955 ---------- NL: split string `input` based on occurrences of regex pattern '[ ](?=[A-Z]+\\b)' ref: re.split('[ ](?=[A-Z]+\\b)', input) Output: re.split('(?<=\\w+)', input) bleu_metric: 39.847572134903544 ---------- NL: Split string `input` at every space followed by an upper-case letter ref: re.split('[ ](?=[A-Z])', input) Output: re.split('(?<=\\w)([A-Z])', input) bleu_metric: 59.75969817602503 ---------- NL: send multipart encoded file `files` to url `url` with headers `headers` and metadata `data` ref: r = requests.post(url, files=files, headers=headers, data=data) Output: urllib.request.urlretrieve('http://www.com/go/') bleu_metric: 8.688932567491825 ---------- NL: write bytes `bytes_` to a file `filename` in python 3 ref: open('filename', 'wb').write(bytes_) Output: f.write(open('filename.txt', 'wb') bleu_metric: 55.95642461260143 ---------- NL: get a list from a list `lst` with values mapped into a dictionary `dct` ref: [dct[k] for k in lst] Output: print([y for x in lst if 'id' in l]) bleu_metric: 11.75029694362029 ---------- NL: find duplicate names in column 'name' of the dataframe `x` ref: x.set_index('name').index.get_duplicates() Output: df.groupby('name')['name'].index bleu_metric: 34.899812526885086 ---------- NL: truncate float 1.923328437452 to 3 decimal places ref: round(1.923328437452, 3) Output: '{0:.2f}'.format(13.95) bleu_metric: 10.802314890908066 ---------- NL: sort list `li` in descending order based on the date value in second element of each list in list `li` ref: sorted(li, key=lambda x: datetime.strptime(x[1], '%d/%m/%Y'), reverse=True) Output: sorted(li, key=lambda x: order.index(x[2], x[2]]) bleu_metric: 30.17923315990544 ---------- NL: place the radial ticks in plot `ax` at 135 degrees ref: ax.set_rlabel_position(135) Output: ax.set_xlabel('30') bleu_metric: 24.446151121745054 ---------- NL: check if path `my_path` is an absolute path ref: os.path.isabs(my_path) Output: os.path.exists(my_path) bleu_metric: 65.80370064762462 ---------- NL: get number of keys in dictionary `yourdict` ref: len(list(yourdict.keys())) Output: len(dict_dict[key] for key in dict_dict) bleu_metric: 17.194656088289214 ---------- NL: count the number of keys in dictionary `yourdictfile` ref: len(set(open(yourdictfile).read().split())) Output: sum(1 for d in my_list if d.get('key', 1)]) bleu_metric: 9.449865252803164 ---------- NL: pandas dataframe get first row of each group by 'id' ref: df.groupby('id').first() Output: df.groupby('id').first() bleu_metric: 100.0 ---------- NL: split a list in first column into multiple columns keeping other columns as well in pandas data frame ref: pd.concat([df[0].apply(pd.Series), df[1]], axis=1) Output: df.apply(lambda x: df.split(',')[1:-1]) bleu_metric: 13.32809869264623 ---------- NL: extract attributes 'src="js/([^"]*\\bjquery\\b[^"]*)"' from string `data` ref: re.findall('src="js/([^"]*\\bjquery\\b[^"]*)"', data) Output: re.findall('[a-zA-Z][\\w-]*\\.\\S*\\S*[^a- bleu_metric: 22.84852347284557 ---------- NL: Sum integers contained in strings in list `['', '3.4', '', '', '1.0']` ref: sum(int(float(item)) for item in [_f for _f in ['', '3.4', '', '', '1.0'] if _f]) Output: sum(map(int, ['1', '3', '2', '3']) bleu_metric: 15.327565138128369 ---------- NL: Call a subprocess with arguments `c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat` that may contain spaces ref: subprocess.Popen(['c:\\Program Files\\VMware\\VMware Server\\vmware-cmd.bat']) Output: subprocess.call('start /home/bin/bash -c "echo "echo "dev/subdir/* somedir/', shell bleu_metric: 6.784909540835388 ---------- NL: reverse a priority queue `q` in python without using classes ref: q.put((-n, n)) Output: (::2 > 0).objects.filter(id=123)) bleu_metric: 13.78592993183041 ---------- NL: make a barplot of data in column `group` of dataframe `df` colour-coded according to list `color` ref: df['group'].plot(kind='bar', color=['r', 'g', 'b', 'r', 'g', 'b', 'r']) Output: df.plot(x, y, 'B') bleu_metric: 1.7617020801492098 ---------- NL: find all matches of regex pattern '([a-fA-F\\d]{32})' in string `data` ref: re.findall('([a-fA-F\\d]{32})', data) Output: print(re.findall('(?=(a.*?)\\b', '\\1', data)) bleu_metric: 29.06614574259767 ---------- NL: Get the length of list `my_list` ref: len(my_list) Output: len(my_list) bleu_metric: 100.0 ---------- NL: Getting the length of array `l` ref: len(l) Output: len(l) bleu_metric: 100.0 ---------- NL: Getting the length of array `s` ref: len(s) Output: len(s) bleu_metric: 100.0 ---------- NL: Getting the length of `my_tuple` ref: len(my_tuple) Output: len(my_list) bleu_metric: 45.180100180492246 ---------- NL: Getting the length of `my_string` ref: len(my_string) Output: len(my_string) bleu_metric: 100.0 ---------- NL: remove escape character from string "\\a" ref: """\\a""".decode('string_escape') Output: print(' '.join(a) bleu_metric: 14.244775721428216 ---------- NL: replace each 'a' with 'b' and each 'b' with 'a' in the string 'obama' in a single pass. ref: """obama""".replace('a', '%temp%').replace('b', 'a').replace('%temp%', 'b') Output: """a b""".replace('b', 'a', 'baz')) bleu_metric: 34.4125014551951 ---------- NL: remove directory tree '/folder_name' ref: shutil.rmtree('/folder_name') Output: os.makedirs(path) bleu_metric: 18.094495256969623 ---------- NL: create a new column `weekday` in pandas data frame `data` based on the values in column `my_dt` ref: data['weekday'] = data['my_dt'].apply(lambda x: x.weekday()) Output: data['hour'] = df['Date'].apply(lambda x: x.year)) bleu_metric: 54.038193355462695 ---------- NL: reverse sort Counter `x` by values ref: sorted(x, key=x.get, reverse=True) Output: sorted(x, key=x.getter(x)) bleu_metric: 55.573034753918414 ---------- NL: reverse sort counter `x` by value ref: sorted(list(x.items()), key=lambda pair: pair[1], reverse=True) Output: sorted(x, key=x.get) bleu_metric: 8.425741883707015 ---------- NL: append a numpy array 'b' to a numpy array 'a' ref: np.vstack((a, b)) Output: a.reshape(a, -1, a1) bleu_metric: 23.901088824528134 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: print(concatenate((a, b), axis=0)) Output: print(a.shape[1, 3, 4]) bleu_metric: 15.325316503089068 ---------- NL: numpy concatenate two arrays `a` and `b` along the second axis ref: print(concatenate((a, b), axis=1)) Output: print(concatenate((a, b), axis=1)) bleu_metric: 100.0 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: c = np.r_[(a[None, :], b[None, :])] Output: print(a.shape[1, 3, 4]) bleu_metric: 9.146501935450095 ---------- NL: numpy concatenate two arrays `a` and `b` along the first axis ref: np.array((a, b)) Output: print(a.shape[1, 3, 4]) bleu_metric: 15.911783110981517 ---------- NL: fetch address information for host 'google.com' ion port 80 ref: print(socket.getaddrinfo('google.com', 80)) Output: socket.send('HTTP/1.0 200 OK') bleu_metric: 15.850829785188195 ---------- NL: add a column 'day' with value 'sat' to dataframe `df` ref: df.xs('sat', level='day', drop_level=False) Output: df['d'].apply(lambda x: 'Y')) bleu_metric: 11.454635531478054 ---------- NL: return a 401 unauthorized in django ref: return HttpResponse('Unauthorized', status=401) Output: return HttpResponse(status=204) bleu_metric: 32.28559902619133 ---------- NL: Flask set folder 'wherever' as the default template folder ref: Flask(__name__, template_folder='wherever') Output: app.path.join(__file__, os.abspath(__file__)) bleu_metric: 11.502783619900047 ---------- NL: How do I INSERT INTO t1 (SELECT * FROM t2) in SQLAlchemy? ref: session.execute('INSERT INTO t1 (SELECT * FROM t2)') Output: session.commit() bleu_metric: 6.117046115680378 ---------- NL: sort a list of lists 'c2' such that third row comes first ref: c2.sort(key=lambda row: row[2]) Output: sorted(lst, key=lambda x: x[2])) bleu_metric: 32.22538601891173 ---------- NL: Sorting a list of lists in Python ref: c2.sort(key=lambda row: (row[2], row[1], row[0])) Output: [sorted(item) for item in data] bleu_metric: 3.9177831075306493 ---------- NL: Sorting a list of lists in Python ref: c2.sort(key=lambda row: (row[2], row[1])) Output: [sorted(item) for item in data] bleu_metric: 5.9428726447571005 ---------- NL: set font `Arial` to display non-ascii characters in matplotlib ref: matplotlib.rc('font', **{'sans-serif': 'Arial', 'family': 'sans-serif'}) Output: matplotlib.rcParams['font.update({'font.size': 'C','size'}) bleu_metric: 21.87310059835298 ---------- NL: Convert DateTime column 'date' of pandas dataframe 'df' to ordinal ref: df['date'].apply(lambda x: x.toordinal()) Output: df['date'] = df['date'].dt.date.tolist() bleu_metric: 35.13874939965221 ---------- NL: get html source of Selenium WebElement `element` ref: element.get_attribute('innerHTML') Output: driver.find_element_by_xpath("//*[contains(text(), 'text')]") bleu_metric: 12.173115521158184 ---------- NL: Get the integer location of a key `bob` in a pandas data frame ref: df.index.get_loc('bob') Output: df.columns = df['state'].get_dict() bleu_metric: 15.76777026245066 ---------- NL: open a 'gnome' terminal from python script and run 'sudo apt-get update' command. ref: os.system('gnome-terminal -e \'bash -c "sudo apt-get update; exec bash"\'') Output: os.system('start "$file"') bleu_metric: 14.726128825673209 ---------- NL: add an item with key 'third_key' and value 1 to an dictionary `my_dict` ref: my_dict.update({'third_key': 1}) Output: default_dict.update({'key1': 1, 'position': 2}) bleu_metric: 35.95818888322724 ---------- NL: declare an array ref: my_list = [] Output: intarray = array('i') bleu_metric: 15.619699684601276 ---------- NL: Insert item `12` to a list `my_list` ref: my_list.append(12) Output: my_list.insert(0, k) bleu_metric: 24.446151121745054 ---------- NL: add an entry 'wuggah' at the beginning of list `myList` ref: myList.insert(0, 'wuggah') Output: a.append(c) bleu_metric: 13.597602315271134 ---------- NL: convert a hex-string representation to actual bytes ref: """\\xF3\\xBE\\x80\\x80""".replace('\\x', '').decode('hex') Output: binascii.unhexlify('\x00\x80')[2:] bleu_metric: 4.9663342844865035 ---------- NL: select the last column of dataframe `df` ref: df[df.columns[-1]] Output: df.drop(df.columns[-1]] bleu_metric: 69.30977286178778 ---------- NL: get the first value from dataframe `df` where column 'Letters' is equal to 'C' ref: df.loc[df['Letters'] == 'C', 'Letters'].values[0] Output: df[df['A'] == 'B'] bleu_metric: 25.294817005134707 ---------- NL: converting two lists `[1, 2, 3]` and `[4, 5, 6]` into a matrix ref: np.column_stack(([1, 2, 3], [4, 5, 6])) Output: np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) bleu_metric: 53.858386640481605 ---------- NL: get the type of `i` ref: type(i) Output: print(isinstance(a)) bleu_metric: 22.679164443904003 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: determine the type of variable `v` ref: type(v) Output: type(v) bleu_metric: 100.0 ---------- NL: get the type of variable `variable_name` ref: print(type(variable_name)) Output: print(get_name) bleu_metric: 24.795364698947967 ---------- NL: get the 5th item of a generator ref: next(itertools.islice(range(10), 5, 5 + 1)) Output: next(it) bleu_metric: 2.7474047213893544 ---------- NL: Print a string `word` with string format ref: print('"{}"'.format(word)) Output: print('Hello, {0}'.format(word)) bleu_metric: 59.978201631280236 ---------- NL: join a list of strings `list` using a space ' ' ref: """ """.join(list) Output: [')','','','','','','','','','','','','','', ' bleu_metric: 5.987016463560546 ---------- NL: create list `y` containing two empty lists ref: y = [[] for n in range(2)] Output: listy = [[] for i in range(3)] bleu_metric: 49.35578819979933 ---------- NL: read a file 'C:/name/MyDocuments/numbers' into a list `data` ref: data = [line.strip() for line in open('C:/name/MyDocuments/numbers', 'r')] Output: data = [int(i) for i in f] bleu_metric: 6.360743006525592 ---------- NL: delete all occurrences of character 'i' in string 'it is icy' ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: re.sub('i', '', 'I', 'it is icy') bleu_metric: 22.408895662889165 ---------- NL: delete all instances of a character 'i' in a string 'it is icy' ref: re.sub('i', '', 'it is icy') Output: re.sub('i', '', 'it is icy') bleu_metric: 100.0 ---------- NL: delete all characters "i" in string "it is icy" ref: """it is icy""".replace('i', '') Output: re.sub('[^i]', '', 'I', text) bleu_metric: 22.722445488928127 ---------- NL: How to delete all instances of a character in a string in python? ref: """""".join([char for char in 'it is icy' if char != 'i']) Output: """""".join([i for i in range(100) if i!= 'i']) bleu_metric: 59.20060650358378 ---------- NL: Drop rows of pandas dataframe `df` having NaN in column at index "1" ref: df.dropna(subset=[1]) Output: df.drop(df.index[[1, 3]], inplace=True) bleu_metric: 12.91802583371725 ---------- NL: get elements from list `myList`, that have a field `n` value 30 ref: [x for x in myList if x.n == 30] Output: myList = [i for i in range(10) if i % 2 == 0] bleu_metric: 12.58220555845937 ---------- NL: converting list of strings `intstringlist` to list of integer `nums` ref: nums = [int(x) for x in intstringlist] Output: [int(x) for x in intstringlist] bleu_metric: 83.9234989944319 ---------- NL: convert list of string numbers into list of integers ref: map(int, eval(input('Enter the unfriendly numbers: '))) Output: [int(i) for i in str_list] bleu_metric: 7.290792403968443 ---------- NL: print "." without newline ref: sys.stdout.write('.') Output: print('\n'.join(str(x)) bleu_metric: 15.911783110981517 ---------- NL: round off the float that is the product of `2.52 * 100` and convert it to an int ref: int(round(2.51 * 100)) Output: int(math.pack('100', struct.pack('100', int(2))) bleu_metric: 12.920725243713852 ---------- NL: Find all files in directory "/mydir" with extension ".txt" ref: os.chdir('/mydir') for file in glob.glob('*.txt'): pass Output: for file in os.listdir('mydir') if os.endswith('.txt'): if file.end bleu_metric: 31.41808768186965 ---------- NL: Find all files in directory "/mydir" with extension ".txt" ref: for file in os.listdir('/mydir'): if file.endswith('.txt'): pass Output: for file in os.listdir('mydir') if os.endswith('.txt'): if file.end bleu_metric: 74.59429832584496 ---------- NL: Find all files in directory "/mydir" with extension ".txt" ref: for (root, dirs, files) in os.walk('/mydir'): for file in files: if file.endswith('.txt'): pass Output: for file in os.listdir('mydir') if os.endswith('.txt'): if file.end bleu_metric: 38.5828207641196 ---------- NL: plot dataframe `df` without a legend ref: df.plot(legend=False) Output: plt.legend(loc=2, fontsize='y') bleu_metric: 13.380161378318956 ---------- NL: loop through the IP address range "192.168.x.x" ref: for i in range(256): for j in range(256): ip = ('192.168.%d.%d' % (i, j)) print(ip) Output: for i in range(0, 10) bleu_metric: 2.18006837039135 ---------- NL: loop through the IP address range "192.168.x.x" ref: for (i, j) in product(list(range(256)), list(range(256))): pass Output: for i in range(0, 10) bleu_metric: 3.8730052084654263 ---------- NL: loop through the IP address range "192.168.x.x" ref: generator = iter_iprange('192.168.1.1', '192.168.255.255', step=1) Output: for i in range(0, 10) bleu_metric: 2.8194161881384923 ---------- NL: Sum the corresponding decimal values for binary values of each boolean element in list `x` ref: sum(1 << i for i, b in enumerate(x) if b) Output: sum(map(int, x)) bleu_metric: 10.55672713033355 ---------- NL: write multiple strings `line1`, `line2` and `line3` in one line in a file `target` ref: target.write('%r\n%r\n%r\n' % (line1, line2, line3)) Output: f.write('\n') bleu_metric: 8.523846965585486 ---------- NL: Convert list of lists `data` into a flat list ref: [y for x in data for y in (x if isinstance(x, list) else [x])] Output: [item for sublist in l for item in sublist] bleu_metric: 6.735737120156013 ---------- NL: Print new line character as `\n` in a string `foo\nbar` ref: print('foo\nbar'.encode('string_escape')) Output: 'Hello\n\n'.rstrip('\n') bleu_metric: 16.15322840648722 ---------- NL: remove last comma character ',' in string `s` ref: """""".join(s.rsplit(',', 1)) Output: s = s.replace(',', '') bleu_metric: 24.4545047029932 ---------- NL: calculate the mean of each element in array `x` with the element previous to it ref: (x[1:] + x[:-1]) / 2 Output: x = np.mean(a, axis=1) bleu_metric: 9.241605197022109 ---------- NL: get an array of the mean of each two consecutive values in numpy array `x` ref: x[:-1] + (x[1:] - x[:-1]) / 2 Output: np.mean(np.arange(len(x)) + 3).mean(axis=1)) bleu_metric: 7.6047020452233385 ---------- NL: load data containing `utf-8` from file `new.txt` into numpy array `arr` ref: arr = numpy.fromiter(codecs.open('new.txt', encoding='utf-8'), dtype='