9879ypxkj
9879ypxkj

Reputation: 437

Can somebody explain me how do the "!" and "%%" commands work in python?

I'm reading a google-cloud-platform notebook tutorial, and I'm encountering some code that seems to be unordinary that I've already met elsewhere but I can't seem to understand what to call it and how it works.

For example:

!head -10 taxi-valid.csv

Gives output:

14.9,2010-11-14 21:50:34 UTC,-73.947907,40.774567,-74.001161,40.739204,1,0
6.5,2009-08-13 20:16:04 UTC,-73.985204,40.742942,-74.004048,40.742819,1,1
6.5,2009-12-19 03:53:33 UTC,-73.985495,40.735795,-73.997268,40.717947,1,2
13.5,2015-04-27 21:37:36 UTC,-73.99408721923828,40.74604034423828,-73.98101806640625,40.7883186340332,1,3
16.5,2012-03-04 00:57:00 UTC,-73.951538,40.713562,-74.001433,40.731157,1,4
8.0,2015-02-03 22:32:35 UTC,-73.9746322631836,40.76266098022461,-73.986083984375,40.74405288696289,6,5
5.3,2009-11-13 07:20:07 UTC,-73.998216,40.745147,-73.989627,40.735806,1,6
9.5,2014-04-15 20:43:11 UTC,-73.993072,40.744764,-73.998106,40.761694,1,7
7.7,2011-10-05 08:14:00 UTC,-73.987945,40.745927,-73.987945,40.745927,5,8
15.0,2014-08-26 07:24:38 UTC,-73.982292,40.727795,-74.00833,40.747368,1,9

or

!ls -l *.csv

gives output:

-rw-r--r-- 1 jupyter jupyter 123398 May 19 19:37 taxi-test.csv
-rw-r--r-- 1 jupyter jupyter 579377 May 19 19:37 taxi-train.csv
-rw-r--r-- 1 jupyter jupyter 122984 May 19 19:37 taxi-valid.csv

Where can I read about this way to give commands?

Or when it loads the data, calls the dataset with "%%":

%%bigquery trips
  SELECT
    FORMAT_TIMESTAMP("%Y-%m-%d %H:%M:%S %Z", pickup_datetime) AS pickup_datetime,
    pickup_longitude, pickup_latitude, 
    dropoff_longitude, dropoff_latitude,
    passenger_count,
    trip_distance,
    tolls_amount,
    fare_amount,
    total_amount
  FROM
    `nyc-tlc.yellow.trips`
  WHERE
    ABS(MOD(FARM_FINGERPRINT(CAST(pickup_datetime AS STRING)), 100000)) = 1
    AND trip_distance > 0 AND fare_amount >= 2.5

How should I interpret the command:

 %%bigquery trips

Upvotes: 2

Views: 81

Answers (1)

Bobby Ocean
Bobby Ocean

Reputation: 3328

The "!" and "%" and "%%" are special commands for an ipython session.

In particular, "!" is used to pass your command from ipython to your shell. This allows you to run things like chmod, grep, ls, md5sum, or any other linux binary inside your ipython session.

The "%" command is known as a magic command that helps make using ipython easier. For example, if you copy code, you can %paste it into ipython. You can also run timers, like "%time sum([1,2,3,4,5])" (for a single test) or %timeit (for thousands of loops). You can read about all the magic commands with %magic. The "%%" is for multi-line magic.

Upvotes: 2

Related Questions