image/svg+xml
FREE PALESTINE
CommandLineFu مع Python _ امثلة بايثون

CommandLineFu مع Python _ امثلة بايثون

ملخص

واحدة من أفضل الطرق لممارسة برمجة Python هي دراسة بعض الأكواد وتجربتها بنفسك. من خلال القيام بالكثير من تمارين الكود ، ستحصل على فهم أفضل لما تفعله بالفعل.

وبعبارة أخرى ، التعلم عن طريق العمل. لمساعدتك في تحسين مهارة ترميز Python الخاصة بك ، قمت بإنشاء برنامج أدناه يستخدم واجهة برمجة التطبيقات منه CommandLineFu.com

واجهة برمجة تطبيقات CommandLineFu

تتمثل الخطوة الأولى الشائعة عندما تريد استخدام خدمات تستند إلى الويب في معرفة ما إذا كانت تحتوي على واجهة برمجة تطبيقات أم لا.

لحسن الحظ بالنسبة لي ، Commandlinefu.com يوفر واحد ويمكن العثور عليه هنا:

“يتوفر محتوى commandlinefu.com في مجموعة متنوعة من التنسيقات المختلفة لتتمكن من القيام بما تريد. يمكن إرجاع أي صفحة تحتوي على قائمة بالأوامر (مثل القوائم حسب العلامة أو الوظيفة أو المستخدم) بتنسيق من اختيارك من خلال تغيير بسيط في عنوان URL للطلب “.

مثال عنوان URL الذي يقدمونه هو:

http://www.commandlinefu.com/commands/’command-set’/’format’/

حيث: command-set هو مكون URL الذي يحدد مجموعة الأوامر التي سيتم إرجاعها.

القيم الممكنة هي:

  • تصفح / فرز حسب الأصوات
  • الموسومة / 163 / grep
  • مطابقة / ssh / c3

التنسيق هو واحد مما يلي:

  • نص عادي
  • json
  • آر إس إس

أفضل استخدام تنسيق json وهذا أيضًا ما أستخدمه في البرنامج أدناه.

إنشاء “أداة بحث سطر الأوامر”

أعتقد أن لدينا جميع معلومات واجهة برمجة التطبيقات التي نحتاجها ، لذا دعنا نصل إليها. البرنامج موثق جيدًا ويجب أن يكون مباشرًا.

افتح محرر نصوص ، انسخ والصق الرمز أدناه. احفظ الملف باسم: “commandlinefu.py” واخرج من المحرر.

#!/usr/bin/env python27
import urllib2
import base64
import json
import os
import sys
import re

os.system("clear")
print "-" * 80
print "Command Line Search Tool"
print "-" * 80

def Banner(text):
    print "=" * 70
    print text
    print "=" * 70
    sys.stdout.flush()

def sortByVotes():
    Banner('Sort By Votes')
    url = "http://www.commandlinefu.com/commands/browse/sort-by-votes/json"
    request = urllib2.Request(url)
    response = json.load(urllib2.urlopen(request))
    #print json.dumps(response,indent=2)
    for c in response:
        print "-" * 60
        print c['command']

def sortByVotesToday():
    Banner('Printing All commands the last day (Sort By Votes) ')
    url = "http://www.commandlinefu.com/commands/browse/last-day/sort-by-votes/json"
    request = urllib2.Request(url)
    response = json.load(urllib2.urlopen(request))
    for c in response:
        print "-" * 60
        print c['command']

def sortByVotesWeek():
    Banner('Printing All commands the last week (Sort By Votes) ')
    url = "http://www.commandlinefu.com/commands/browse/last-week/sort-by-votes/json"
    request = urllib2.Request(url)
    response = json.load(urllib2.urlopen(request))
    for c in response:
        print "-" * 60
        print c['command']

def sortByVotesMonth():
    Banner('Printing: All commands from the last months (Sorted By Votes) ')
    url = "http://www.commandlinefu.com/commands/browse/last-month/sort-by-votes/json"
    request = urllib2.Request(url)
    response = json.load(urllib2.urlopen(request))
    for c in response:
        print "-" * 60
        print c['command']

def sortByMatch():
    #import base64
    Banner("Sort By Match")
    match = raw_input("Please enter a search command: ")
    bestmatch = re.compile(r' ')
    search = bestmatch.sub('+', match)
    b64_encoded = base64.b64encode(search)
    url = "http://www.commandlinefu.com/commands/matching/" + search + "https://www.pythonforbeginners.com/" + b64_encoded + "/json"
    request = urllib2.Request(url)
    response = json.load(urllib2.urlopen(request))
    for c in response:
        print "-" * 60
  print c['command']

print """
1. Sort By Votes (All time)
2. Sort By Votes (Today)
3. Sort by Votes (Week)
4. Sort by Votes (Month)
5. Search for a command
 
Press enter to quit
"""

while True:
  answer = raw_input("What would you like to do? ")

 if answer == "":
    sys.exit()
  
  elif answer == "1":
   sortByVotes()
 
  elif answer == "2":
   print sortByVotesToday()
  
  elif answer == "3":
   print sortByVotesWeek()
 
  elif answer == "4":
   print sortByVotesMonth()
  
  elif answer == "5":
   print sortByMatch()
 
  else:
   print "Not a valid choice"
Code language: PHP (php)

عند تشغيل البرنامج ، ستظهر لك قائمة يمكنك من خلالها تحديد اختياراتك.


--------------------------------------------------------------------------------
Command Line Search Tool
--------------------------------------------------------------------------------

1. Sort By Votes (All time)
2. Sort By Votes (Today)
3. Sort by Votes (Week)
4. Sort by Votes (Month)
5. Search for a command

Press enter to quit

What would you like to do?
...
...
Code language: JavaScript (javascript)

لا يوجد اعجابات