xml地图|网站地图|网站标签 [设为首页] [加入收藏]
常用查询操作符,非中文自动翻译
分类:编程

 

步骤:1、新建信号量对象,设置信号量数量
2、线程执行方法中,如果能够获取信号量,则执行,信号量为0不可以获取

 

声明:我写的所有文章都是发在博客园的,我看到其他复制粘贴过去的 连个出处也不写,直接打上自己的水印。。。真是没的说了。

图片 1图片 2

 这一篇介绍了下面的内容:

前言:前段时间搞了一些爬视频的项目,代码都写好了,这里写文章那就在来重新分析一遍吧。有不好的地方 莫见怪 : )

 1 import  threading,time
 2 
 3 class  MyThread(threading.Thread):
 4     def  run(self):
 5         if  sema.acquire():
 6             print(self.name)
 7             time.sleep(0.3)
 8             sema.release()
 9 
10 if __name__ == '__main__':
11     sema = threading.BoundedSemaphore(5)  # sema=threading.Semaphore(5)
12     thrs = []
13     for i in range(100):
14         thrs.append(MyThread())
15     for t in thrs:
16         t.start()
  • 查询object数组
  • 查询强类型数组
  • 查询泛型字典
  • 查询字符串
  • SelectMany
  • 索引
  • Distinct操作符
  • 排序
  • 嵌套查询
  • 分组
  • 组连接
  • 内连接
  • 左外连接
  • 交叉连接
  • skip,take

环境:python2.7 + win10

View Code

 

 

 

详细请参看代码.

开始先说一下,访问youtube需要科学上网,请自行解决,最好是全局代理。

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace WindowsFormsApplication1
 11 {
 12     public partial class Form1 : Form
 13     {
 14         public Form1()
 15         {
 16             InitializeComponent();
 17         }
 18 
 19         private void Form1_Load(object sender, EventArgs e)
 20         {
 21             
 22         }
 23 
 24         private void button1_Click(object sender, EventArgs e)
 25         {
 26             //查询object数组
 27             Object[] array = { "String", 12, true, 'a' };
 28             var types = array
 29                 .Select(s => s.GetType().Name)
 30                 .OrderBy(s => s);
 31             ObjectDumper.Write(types);
 32         }
 33 
 34         private void button2_Click(object sender, EventArgs e)
 35         {
 36             //查询强类型数组
 37             Book[] books ={
 38                 new Book{Title="Linq in Action"},
 39                 new Book{Title="Linq for Fun"},
 40                 new Book{Title="Extreme LINQ"}
 41             };
 42 
 43             var titles =
 44                 books
 45                 .Where(s => s.Title.Contains("Action"))
 46                 .Select(s => s.Title);
 47 
 48             ObjectDumper.Write(titles);
 49         }
 50 
 51         private void button3_Click(object sender, EventArgs e)
 52         {
 53             //查询泛型字典
 54             var dic = new Dictionary<int, string>();
 55             dic.Add(0, "zero");
 56             dic.Add(1, "un");
 57             dic.Add(2, "deux");
 58             dic.Add(3, "trois");
 59             dic.Add(4, "quatre");
 60             var list =
 61                 dic
 62                 .Where(s => s.Key % 2 == 0)
 63                 .Select(s => s.Value);
 64 
 65             ObjectDumper.Write(list);
 66         }
 67 
 68         private void button4_Click(object sender, EventArgs e)
 69         {
 70             //查询字符串, 字符串实现了IEnumerable<Char>接口
 71             var count = "Non-letter characters in this string: 6"
 72                 .Where(s => !Char.IsLetter(s))
 73                 .Count();
 74 
 75             ObjectDumper.Write(count);
 76         }
 77 
 78         private void button5_Click(object sender, EventArgs e)
 79         {
 80             string[] books ={"Funny Stories","All your base are belong to us",
 81                                "LINQ rules","C# on Rails","Bonjour mon Amour"};
 82             var query =
 83                 books
 84                     .Where(s => s.Length > 10)
 85                     .Select(s => new { Book = s.ToUpper() });
 86 
 87             var query1 =
 88                 from s in books
 89                 where s.Length > 10
 90                 orderby s
 91                 select new { Book = s.ToUpper() };
 92 
 93             dataGridView1.DataSource = query1.ToList();
 94         }
 95 
 96         private void button6_Click(object sender, EventArgs e)
 97         {
 98             var query =
 99                 from s in SampleData.Books
100                 where s.Title.Length > 5
101                 orderby s.Title
102                 select new { Book = s.Title, Price = s.Price };
103 
104             dataGridView2.DataSource = query.ToList();
105         }
106 
107         private void button7_Click(object sender, EventArgs e)
108         {
109             //SelectMany
110 
111             var query1 =
112                 SampleData.Books
113                 .SelectMany(s => s.Authors);
114             ObjectDumper.Write(query1);
115 
116             var query =
117                 from book in SampleData.Books
118                 from author in book.Authors
119                 select author.FirstName + author.LastName;
120 
121             ObjectDumper.Write(query);
122         }
123 
124         private void button8_Click(object sender, EventArgs e)
125         {
126             var book =
127                 SampleData.Books
128                     .Select((s, no) => new { no, s.Title })
129                     .OrderBy(s => s.Title);
130 
131             //无法翻译为等同的查询表达式语法
132 
133             ObjectDumper.Write(book);
134                     
135         }
136 
137         private void button9_Click(object sender, EventArgs e)
138         {
139             //Distinct操作符
140             var query =
141                 SampleData.Books
142                     .SelectMany(s => s.Authors)
143                     .Select(s => s.FirstName + s.LastName);
144             ObjectDumper.Write(query);
145 
146             //去除重复的author,C#没有和distinct等同的查询表达式
147             var query1 =
148                 SampleData.Books
149                     .SelectMany(s => s.Authors)
150                     .Distinct()
151                     .Select(s => s.FirstName + s.LastName);
152             Console.WriteLine();
153             ObjectDumper.Write(query1);
154 
155         }
156 
157         private void button10_Click(object sender, EventArgs e)
158         {
159             //排序
160             var query =
161                 from s in SampleData.Books
162                 orderby s.Publisher.Name, s.Price descending, s.Title
163                 select new
164                 {
165                     Publisher=s.Publisher.Name,
166                     s.Price,
167                     s.Title
168                 };
169             dataGridView1.DataSource = query.ToList();
170             ObjectDumper.Write(query);
171 
172             var query1 =
173                 SampleData.Books
174                 .OrderBy(s => s.Publisher.Name)
175                 .ThenByDescending(s => s.Price)
176                 .ThenBy(s => s.Title)
177                 .Select(s => new
178                     {
179                         publisher = s.Publisher.Name,
180                         s.Price,
181                         s.Title
182                     });
183             Console.WriteLine();
184             ObjectDumper.Write(query1);
185            
186         }
187 
188         private void button11_Click(object sender, EventArgs e)
189         {
190             //嵌套查询
191 
192             var query =
193                 from s in SampleData.Publishers
194                 select new
195                 {
196                     item1 = s.Name,
197                     item2 =
198                         from book in SampleData.Books
199                         where book.Publisher.Name == s.Name
200                         select book
201                 };
202 
203             foreach (var m in query)
204             {
205                 Console.WriteLine(m.item1+":");
206                 foreach (var k in m.item2)
207                 {
208                     Console.WriteLine(k.Title);
209                 }
210                 Console.WriteLine();
211             }
212             
213 
214         }
215 
216         private void button12_Click(object sender, EventArgs e)
217         {
218             //分组
219             //代码让图书按出版社分组.属于同一出版社的图书将被分到相同的组中.
220             //在这个查询中,该分组publisherBooks对象是IGrouping<TKey,T>接口.
221             //其中T为集合IEnumerable<Book>
222 
223             var query =
224                 from s in SampleData.Books
225                 group s by s.Publisher into publisherBooks
226                 select new
227                 {
228                     publisher=publisherBooks.Key.Name,
229                     books=publisherBooks,
230                     booksum=publisherBooks.Count()
231                 };
232             foreach (var m in query)
233             {
234                 Console.WriteLine(m.publisher + ":");
235                 foreach (var k in m.books)
236                 {
237                     Console.WriteLine(k.Title);
238                 }
239                 Console.WriteLine(m.booksum);
240                 Console.WriteLine();
241             }
242              
243         }
244 
245         private void button13_Click(object sender, EventArgs e)
246         {
247             //组连接
248             var query =
249                 from s in SampleData.Publishers
250                 join book in SampleData.Books
251                 on s equals book.Publisher into publisherBooks
252                 select new
253                 {
254                     publisher=s.Name,
255                     books=publisherBooks
256                 };
257 
258             foreach (var m in query)
259             {
260                 Console.WriteLine(m.publisher + ":");
261                 foreach (var k in m.books)
262                 {
263                     Console.WriteLine(k.Title);
264                 }
265                 Console.WriteLine();
266             }
267 
268         }
269 
270         private void button14_Click(object sender, EventArgs e)
271         {
272             //内连接,旨在找到两个序列的交集
273             //和组连接的差别是,没有使用into关键字将元素分组,而是将图书投影在了出版社对象上
274             //注意没有任何书籍的出版社没有显示出来,这是因为在两个待连接序列中均符合条件的组合才会出现在结果序列中.
275             var query =
276                 from s in SampleData.Publishers
277                 join book in SampleData.Books on s equals book.Publisher
278                 select new
279                 {
280                     publisher=s.Name,
281                     books=book.Title
282                 };
283 
284             var query2=
285                 SampleData.Publishers
286                     .Join(SampleData.Books,     //内部序列
287                         s=>s,                   //外部的key选择器
288                         book=>book.Publisher,   //内部的key选择器
289                         (s,book)=>new           //结果选择器
290                         {
291                             publisher=s.Name,
292                             books=book.Title
293                         });
294 
295             ObjectDumper.Write(query2);
296 
297             foreach (var m in query)
298             {
299                 Console.WriteLine(m.publisher + ":");
300                 Console.WriteLine(m.books);
301                 Console.WriteLine();
302             }
303 
304         }
305 
306         private void button15_Click(object sender, EventArgs e)
307         {
308             //左外连接
309             var query =
310                 from s in SampleData.Publishers
311                 join book in SampleData.Books
312                     on s equals book.Publisher into publisherBooks
313                 from book in publisherBooks.DefaultIfEmpty()            //为空序列提供默认元素
314                 select new
315                 {
316                     publisher=s.Name,
317                     books=book==default(Book)?"(no books)":book.Title
318                 };
319 
320             ObjectDumper.Write(query);
321         }
322 
323         private void button16_Click(object sender, EventArgs e)
324         {
325             //交叉连接, 计算出两个序列中所有元素的积
326             //结果序列由一个序列的每个元素和另一个序列中每个元素的完全组合构成
327             var query =
328                 from s in SampleData.Publishers
329                 from book in SampleData.Books
330                 select new
331                 {
332                     correct=(s==book.Publisher),
333                     publisher=s.Name,
334                     books=book.Title
335                 };
336             ObjectDumper.Write(query);   
337         }
338 
339         private void button17_Click(object sender, EventArgs e)
340         {
341             //skip,take
342             var list1 = new List<string>();
343             for (int i = 0; i < 100; i++)
344                 list1.Add(i.ToString());
345 
346             var query =
347                 list1
348                 .Select(s=>s)
349                 .Skip(90).Take(10);
350                 
351 
352             ObjectDumper.Write(query);
353         }
354 
355 
356 
357 
358 
359     }
360 
361   
362 }

ok,现在开始,首先打开网站观察

例子:

 

 

### 强行改的信号量

本节源代码下载

图片 3

图片 4图片 5

 

 

 1 import threading
 2 import time
 3 class Tickets:
 4     def __init__(self):
 5     self.ticket = 100
 6 
 7     def sell_ticket(self):
 8         while self.ticket > 0:
 9             if loc.acquire():
10                 print('卖出',self.ticket,'票',threading.current_thread())
11                 self.ticket -= 1
12                 loc.release()
13             time.sleep(0.0001)
14 
15 if __name__ == '__main__':
16     ticket = Tickets()
17     t1 = threading.Thread(target=ticket.sell_ticket)
18     t2 = threading.Thread(target=ticket.sell_ticket)
19     t3 = threading.Thread(target=ticket.sell_ticket)
20     loc = threading.Semaphore(1)
21     t1.start()
22     t2.start()
23     t3.start()

原创文章,出自"博客园, 猪悟能'S博客" : 

 网站很干净清爽,这次做的是基于关键字搜索来爬那些相关视频,这样就能很好的分类了,若输入中文搜索,那结果也一般都是国内视频,英文的话 那就是国外的。

View Code

 

这里先来测试中文的 ,输入''搞笑'',搜出来很多视频,也可以根据条件筛选,YouTube视频链接很有规律,都是这种 v值不一样,这里就叫id吧。

##########38里面多生产多消费信号量实现

 

图片 6图片 7

图片 8

 1 import time
 2 import threading
 3 
 4 class Res:
 5     def __init__(self):
 6         self.flag = False
 7         self.count = 0
 8         self.product = ''
 9 
10     def set(self,name):
11         lock_con.acquire()
12         while self.flag:
13             lock_con.wait()
14         time.sleep(0.00001)
15         self.count += 1
16         self.product = ''.join([name,'**',str(self.count)])
17         self.message = ''.join([self.product,'__生产者__',str(threading.current_thread())])
18         print(self.message)
19         self.flag = True
20         lock_con.notifyAll()
21         lock_con.release()
22 
23     def get_product(self):
24         lock_con.acquire()
25         time.sleep(0.00001)
26         while not self.flag:
27             lock_con.wait()
28         self.message = ''.join([self.product,'__消费者__',str(threading.current_thread())])
29         print(self.message)
30         self.flag = False
31         lock_con.notifyAll()
32         lock_con.release()
33 
34 class Producer(threading.Thread):
35     def __init__(self,r):
36         threading.Thread.__init__(self)
37         self.r = r
38 
39     def run(self):
40         for i in range(100):
41             self.r.set('大白兔奶糖')
42 
43 class Consumer(threading.Thread):
44     def __init__(self,r):
45         threading.Thread.__init__(self)
46         self.r = r
47 
48     def run(self):
49         for i in range(100):
50             self.r.get_product()
51 
52 if __name__ == '__main__':
53     lock_con = threading.Condition()
54     r = Res()
55     l = []
56     for i in range(5):
57         l.append(Consumer(r))
58     for i in range(5):
59         l.append(Producer(r))
60     for a in l:
61         a.start()

 

View Code

ok,先从最简单的开始,查看网页源代码看看这些视频链接是否都是在里面,我睁大了我的24k单身狗的眼睛找出来了。。。看了一下,视频信息全在这个<script>标签里面。

 

 

图片 9

本文由澳门新葡亰手机版发布于编程,转载请注明出处:常用查询操作符,非中文自动翻译

上一篇:很具体很用实用,批量自动重命名音乐文件 下一篇:没有了
猜你喜欢
热门排行
精彩图文