python equivalent of head/grep/tail
Just as UNIX developers did, you'll need to write the supporting functions. head
and tail
are executable programs, not atomic system commands. grep
is even more complex, being the system regular expression facility.
Look up what you need in regex
for Python (or almost any high-level language). For head
and tail
, you'll need to write simply functions that read the file or input stream ... just as UNIX does in the underlying code. These are not built-in commands in general-purpose languages.
Do keep in mind that you're switching paradigms. Shell programming is specifically canted for the central purpose of an OS: managing system resources. When you translate such things to a general-purpose language, you will gain many general-purpose facilities, but lose many powerful routines for direct manipulation of OS objects.
Also look into Python's modules os and system; you may find that it's most effective to simply invoke the OS from within Python:
taco_str = os.system("head -n 14 ...")
taco_str += os.system("grep ...")
taco_str += os.system("tail -n 5 ...")
and the write taco_str
to the desired file.
Python tail-like file read
You can just create a new file for each test and do a seek to the front to get its value. I moved to TemporaryFile
since you don't use the file name.
def test_invalid_args(self):
for args in [['-id', '123456789.00'], ['-i'], ['-i', '0'], ['-i', '../some_invalid_path/not_exist.json']]:
with RedirectedEnv(stderr=tempfile.TemporaryFile()):
try:
self.parser.parse(args)
raise AssertionError("should have failed")
except Exception:
sys.stderr.seek(0)
self.assertTrue(sys.stderr.readline().startswith("usage:"))
Taking list's tail in a Pythonic way?
Alternative ways,
# 1
result = data.count(data[0]) > 1
# 2
it = iter(data)
result = it.next() in it
How can I make the following function into a tail-recursive function?
Instead of doing
if right_best is None:
return bst.val
You can pass the best result found so far into the recursive call as an extra argument, and have the recursive call handle this check.
def find_val_or_next_smallest(bst, x, best=None):
"""
Get the greatest value <= x in a binary search tree.
Returns None if no such value can be found.
"""
if bst is None:
return best
elif bst.val == x:
return x
elif bst.val > x:
return find_val_or_next_smallest(bst.left, x, best)
else:
# bst.val is guaranteed to be the best yet, since if we had
# seen a better value higher up, the recursion would have gone
# the other way from that node
return find_val_or_next_smallest(bst.right, x, bst.val)
Tail and search in txt file
There is no tail
function in the python standard library. What I would suggest is a list slicing approach:
n = 5
lastlines = lst[-n:]
for line in lastlines:
if SEARCH_MASTER in line:
Master='Master_SX'
Further reading can be done here
What is the equivalent of pandas.DataFrame.tail in DataBricks
DataBricks is apparently using pyspark.sql
dataframes, not pandas
.
# Index the df if you haven't already
# Note that monotonically increasing id has size limits
from pyspark.sql.functions import monotonically_increasing_id
df = df.withColumn("index", monotonically_increasing_id())
# Query with the index
tail = sqlContext.sql("""SELECT * FROM df ORDER BY index DESC limit 5""")
tail.show()
Note that this is expensive and doesn't play to the strengths of Spark
.
See also:
https://medium.com/@chris_bour/6-differences-between-pandas-and-spark-dataframes-1380cec394d2
pyspark,spark: how to select last row and also how to access pyspark dataframe by index
Related Topics
Not All Parameters Were Used in the SQL Statement (Python, MySQL)
Numpy.Unique with Order Preserved
How to Remove Gaps Between Subplots in Matplotlib
Listing Available Com Ports with Python
How to Flatten Lists Without Splitting Strings
How to Trigger Function on Value Change
Regular Expressions: Search in List
How to Distribute Python Programs
Str' Object Has No Attribute 'Decode'. Python 3 Error
Bin Elements Per Row - Vectorized 2D Bincount for Numpy
Changing Iteration Variable Inside for Loop in Python
I Expect 'True' But Get 'None'
How to Insert Newlines on Argparse Help Text
How to Write to a File, Using the Logging Python Module
How to Import Members of All Modules Within a Package
Convert a List with Strings All to Lowercase or Uppercase
How to Delete All Blank Lines in the File with the Help of Python