Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-03-08 09:33:03 +08:00
commit efcfe17e19
3 changed files with 270 additions and 40 deletions

View File

@ -1,31 +1,31 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11970-1.html)
[#]: subject: (Use logzero for simple logging in Python)
[#]: via: (https://opensource.com/article/20/2/logzero-python)
[#]: author: (Ben Nuttall https://opensource.com/users/bennuttall)
使用 logzero 在 Python 中进行简单日志记录
======
一个方便的日志库快速入门,来帮助你掌握这个重要的编程概念。
![Snake charmer cartoon with a yellow snake and a blue snake][1]
logzero 库使日志记录就像打印语句一样容易,是简单性的杰出代表。我不确定 logzero 的名称是否要与 pygame-zero、GPIO Zero 和 guizero 这样的 “zero 样板库”契合,但是肯定属于该类别。它是一个 Python 库,使得日志记录变得简单明了
> 快速了解一个方便的日志库,来帮助你掌握这个重要的编程概念。
你可以使用它的基本日志记录到标准输出,就像你可以使用 print 来获得信息和调试一样,它还有学习更高级日志记录(例如记录到文件)的平滑学习曲线。
![](https://img.linux.net.cn/data/attachment/album/202003/07/122445v743hy7ajdyrrda1.jpg)
logzero 库使日志记录就像打印语句一样容易,是简单性的杰出代表。我不确定 logzero 的名称是否要与 pygame-zero、GPIO Zero 和 guizero 这样的 “zero 样板库”契合,但是肯定属于该类别。它是一个 Python 库,可以使日志记录变得简单明了。
你可以使用它基本的记录到标准输出的日志记录,就像你可以使用 print 来获得信息和调试一样,学习它的更高级日志记录(例如记录到文件)的学习曲线也很平滑。
首先,使用 pip 安装 logzero
```
`$ sudo pip3 install logzero`
$ sudo pip3 install logzero
```
在 Python 文件中,导入 logger 并尝试以下一个或所有日志实例:
```
from logzero import logger
@ -39,14 +39,13 @@ logger.error("error")
![Python, Raspberry Pi: import logger][2]
因此现在不要再使用 **print** 来了解发生了什么,而应使用有相关日志级别的 logger
因此现在不要再使用 `print` 来了解发生了什么,而应使用有相关日志级别的日志器
### 在 Python 中将日志写入文件
如果你阅读至此,并会在你写代码时做一点改变,这对我就足够了。如果你要了解更多,请继续阅读!
写到**标准输出**对于测试新程序不错,但是仅当你登录到运行脚本的计算机时才有用。在很多时候,你需要远程执行代码并在事后查看错误。这种情况下,记录到文件很有帮助。让我们尝试一下:
写到标准输出对于测试新程序不错,但是仅当你登录到运行脚本的计算机时才有用。在很多时候,你需要远程执行代码并在事后查看错误。这种情况下,记录到文件很有帮助。让我们尝试一下:
```
from logzero import logger, logfile
@ -54,32 +53,28 @@ from logzero import logger, logfile
logfile('/home/pi/test.log')
```
现在,你的日志条目将记录到文件 **test.log** 中。记住确保[脚本有权限] [3]写入该文件及其目录结构。
现在,你的日志条目将记录到文件 `test.log` 中。记住确保[脚本有权限][3]写入该文件及其目录结构。
你也可以指定更多选项:
```
`logfile(/home/pi/test.log, maxBytes=1e6, backupCount=3)`
logfile('/home/pi/test.log', maxBytes=1e6, backupCount=3)
```
现在,当提供给 **logfile** 文件达到 1MB10^6 字节)时,它将通过 **test.log.1**、**test.log.2** 等文件轮询写入。这种行为可以避免系统打开和关闭大量 I/O 密集的日志文件,以至于系统无法打开和关闭。你或许还要记录到 **/var/log**。假设你使用的是 Linux那么创建一个目录并将用户设为所有者以便可以写入该目录
现在,当提供给 `test.log` 文件的数据达到 1MB10^6 字节)时,它将通过 `test.log.1`、`test.log.2` 等文件轮替写入。这种行为可以避免系统打开和关闭大量 I/O 密集的日志文件,以至于系统无法打开和关闭。更专业一点,你或许还要记录到 `/var/log`。假设你使用的是 Linux那么创建一个目录并将用户设为所有者以便可以写入该目录
```
$ sudo mkdir /var/log/test
$ sudo chown pi /var/log/test
```
然后在你的 Python 代码中,更改 **logfile** 路径:
然后在你的 Python 代码中,更改 `logfile` 路径:
```
`logfile(/var/log/test/test.log, maxBytes=1e6, backupCount=3)`
logfile('/var/log/test/test.log', maxBytes=1e6, backupCount=3)
```
当要在 **logfile** 中捕获异常时,可以使用 **logging.exception**:。
当要在 `logfile` 中捕获异常时,可以使用 `logging.exception`
```
try:
@ -88,8 +83,7 @@ except Exception as e:
    logger.exception(e)
```
这将输出(在 b 为零的情况下):
这将输出(在 `b` 为零的情况下):
```
[E 190422 23:41:59 test:9] division by zero
@ -99,8 +93,7 @@ except Exception as e:
     ZeroDivisionError: division by zero
```
你会得到日志,还有完整回溯。另外,你可以使用 **logging.error** 并隐藏回溯:
你会得到日志,还有完整回溯。另外,你可以使用 `logging.error` 并隐藏回溯:
```
try:
@ -111,28 +104,20 @@ except Exception as e:
现在,将产生更简洁的结果:
```
`[E 190423 00:04:16 test:9] ZeroDivisionError: division by zero`
[E 190423 00:04:16 test:9] ZeroDivisionError: division by zero
```
* * *
* * *
* * *
**![Logging output][4]**
![Logging output][4]
你可以在 [logzero.readthedocs.io] [5] 中阅读更多选项。
### logzero 为教育而生
对于新手程序员来说,日志记录可能是一个具有挑战性的概念。大多数框架依赖于流控制和大量变量操作来生成有意义的日志,但是 logzero不同。由于它的语法类似于 print 语句,因此它在教育上很成功,因为它无需解释其他概念。在你的下个项目中试试它。
对于新手程序员来说,日志记录可能是一个具有挑战性的概念。大多数框架依赖于流控制和大量变量操作来生成有意义的日志,但是 logzero 不同。由于它的语法类似于 `print` 语句,因此它在教育上很成功,因为它无需解释其他概念。在你的下个项目中试试它。
\--
_此文章最初发布在[我的博客] [6]上,经许可重新发布。_
此文章最初发布在[我的博客][6]上,经许可重新发布。
--------------------------------------------------------------------------------
@ -141,7 +126,7 @@ via: https://opensource.com/article/20/2/logzero-python
作者:[Ben Nuttall][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -0,0 +1,115 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (The De-Googled Android Fork is Making Good Progress)
[#]: via: (https://itsfoss.com/gael-duval-interview/)
[#]: author: (Abhishek Prakash https://itsfoss.com/author/abhishek/)
The De-Googled Android Fork is Making Good Progress
======
A couple years ago, we covered the [Eelo project][1]. If you remember, the Eelo project was started by [Gael Duval][2] who once created Mandrake Linux. The goal of the Eelo project was to remove all Google services from Android to give you an [alternate mobile operating system][3] that doesnt track you and invade your privacy.
A lot has happened to Eelo since then. Its not called Eelo anymore, now its called /e/. So, whats happening with this project? We talked to Gael Duval himself. Heres what he shared with us.
![][4]
_**Why did you create this Eelo or /e/ project in the first place?**_
**Gael:** In 2017, I realized that using Android and iPhone, Google and many mobile apps was not compatible with my personal privacy.
A later study by a US University confirmed this: using an iPhone or and Android phone sends between 6 to 12 MB of personal data to Google servers, daily! And this doesnt count mobile apps.
So I looked for reasonable alternatives to iPhone and Android phones but didnt find any. Either I found options for hobbyists, like Ubuntu Touch, that were not compatible with existing apps and not fully unGoogled either. Or there were alternative ROMs with all the Google fat inside, and no associated basic online services that could be used without tweaking the system.
Therefore, an idea came to mind: why not fork Android, remove all the Google features, even low level, such as connectivity check, DNS…, replace default apps with more virtuous apps, add basic online services, and integrate all this into a consistent form that could be used by Mum and Dad and any people without tech or expert knowledge?
_**How is it any different from other custom Android ROMs?**_
**Gael:** It doesnt send a bit of data to Google, and is and will be more and more privacy-focused.
Low-level: we remove any Android feature that sends data to Google servers. Even the connectivity check when you start the smartphone! To my knowledge, there is not any other Android ROM that does this at the moment. We change default DNS settings and offer users an option to set the DNS of their choice. We change NTP (automatic time configuration) settings to the default NTP servers because there is no reason to use Google NTP servers actually. Then we remove Google services, and we replace with a software layout called microG that can still receive push notifications and have geolocation data for apps (using Mozilla geolocation service).
Then we change the default apps by non-Google apps, including the maps applications, mail etc., most are open source applications and I can say that there is 99% probability that all will be open source before the end of this year.
Then we add our own Android application installer, with close to 80 000 available applications at the moment.
We provide a different web browser, which is a fork of Chromium, were all features that data to Google are removed, and were the default search engine is not Google…
And we operate online services:
* search, using a meta-search system that we have improve for a better user experience
* online drive with encrypted data, calendar etc. using a modified version of NextCloud
* mail…
And for we provide a unique identifier that can be used to access all those services, either on the web or from the /e/ OS system, by login once. Then you can sync all your data, calendar, email etc. between your smartphone and your personal /e/ cloud (it can also be self-hosted).
The purpose of the project is to provide a normal, ready to use, and attractive “digital life” to users, without sending all your personal data to Google.
_**If it is completely ungoogled, how do users install new apps? Do you have your own app store? If yes, how can we trust that these apps dont spy on user data?**_
**Gael:** Yes we have our own application installer, with about 80 000 applications. And we analyse each application to unveil the number of trackers, and we display this information to our users, for each application. We are also adding Progressive Web Apps soon to this application installer.
/e/ OS is about freedom of choice. We want the core system to be better, and then offer as many possible options to users, by informing them as much as possible. In short: they can still any application they need. Next step will be to offer a feature to actually block trackers used in applications.
_**What is the target user base for /e/? Can an average Joey use it without much trouble?**_
![][5]
**Gael:** We started with tech-savvy users, and were expanding the user base to people with less knowledge. At the moment, our typical user base is a mix of tech-savvy users, who can flash a smartphone with /e/ OS and people who are very concerned with Google and their data privacy but have very limited technical knowledge. For those people we have some smartphones pre-installed with /e/ OS for sale, on high-grade refurbished hardware.
We are also announcing this week an “/e/ easy installer” that will make the flashing process much more easier, by pluging the smartphone to a PC and launching a dedicated application that will make most of the job.
Then, the next step will be to expand our target users to a more global market, once we find the good partners. But clearly, there is a demand for something different than the Apple-Google worldwide market duopoly on the mobile.
_**Initially the project was named eelo and it is called /e/ or [e foundation][6]. Personally, I find the name /e/ weird and it is not easily recognizable. Why did you change the project name?**_
**Gael:** We have been “attacked” by a company called “eelloo”. They considered that “eelo” would interfere with their business. They are in the HR business solutions, but registered their trademark in all the classes related to mobile OS, smartphones etc. This is silly and a shame, but we had no money to defend us strongly at the time.
However the/e/ name will be abandonned for something else quite soon.
_**Its been a couple of years since the initial launch. How do you see the adoption of /e/?**_
**Gael:** We launched the first beta 18 months ago, and we have started to sell smartphones with /e/ a little more than 6 months ago. The adoption is growing a lot at the moment, we have to add terabytes of online storage regularly!
Also with the /e/ installer arriving, and some official partnerships with some hardware mobile manufacturers in the pipe, this is going to accelerate a lot this year.
However, this is not surprising, privacy concerns are rising both for individuals and corporations, and I think the rejection of Google is also trending.
_**What are your future plans to grow /e/?**_
**Gael:** The growth is very natural. There is a strong community of users who realize how unique our approach is. These guys are contributing, supporting us and talking a lot about the project.
With the easy installer coming along and strategic partnerships with hardware makers, this is going to accelerate a lot.
Also, and this is more personal, I think that there is a natural connection between /e/ OS, and the Linux world. OK, /e/ OS is based on Android, but its still a Linux kernel and its the same spirit, its Open Source… So Id really like to have more natural integration between my /e/ smartphone and my Linux desktop. There should be some nice features added in this spirit in the next versions of /e/ OS.
_**What can /e/ users and our readers do to help e foundation?**_
**Gael:** Join us, talk about what we are doing, send your feedback, organize some meetups… Help improve the /e/ Wikipedia page which is very poor and doesnt represent at all what we are actually doing.
We also have a [permanent crowdfunding campaign where users can support the project financially][7], pay for the servers etc. And, in addition to giving back in term of open source product, we send cool stuff in return :)
--------------------------------------------------------------------------------
via: https://itsfoss.com/gael-duval-interview/
作者:[Abhishek Prakash][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/abhishek/
[b]: https://github.com/lujun9972
[1]: https://itsfoss.com/eelo-mobile-os/
[2]: https://en.wikipedia.org/wiki/Ga%C3%ABl_Duval
[3]: https://itsfoss.com/open-source-alternatives-android/
[4]: https://i0.wp.com/itsfoss.com/wp-content/uploads/2020/03/e-os-interview.jpg?ssl=1
[5]: https://i2.wp.com/itsfoss.com/wp-content/uploads/2020/03/e-foundation-smartphones.jpg?resize=800%2C590&ssl=1
[6]: https://e.foundation/
[7]: https://e.foundation/donate/

View File

@ -0,0 +1,130 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Compose music as code using Sonic Pi)
[#]: via: (https://opensource.com/article/20/3/sonic-pi)
[#]: author: (Matt Bargenquast https://opensource.com/users/mbargenquast)
Compose music as code using Sonic Pi
======
There's no need for instrumental mastery with this accessible open
source program that can turn you into a musical virtuoso.
![Bird singing and music notes][1]
Maybe you're like me, and you learned a musical instrument when you were in school. For me, it was the piano, and later, the viola. However, I've always held that, as my childhood interests shifted towards computers and coding, I subsequently neglected my music practice. I do wonder what I would have done if I'd had something like Sonic Pi when I was younger. Sonic Pi is an open source program that lets you compose and perform music through code itself. It's the perfect marriage of those two worlds.
Opensource.com is no stranger to Sonic Pi—we [featured an interview][2] with the creator, Dr. Sam Aaron, back in 2015. Since that time, a lot has changed, and Sonic Pi has grown substantially in many ways. It's reached a major new version milestone, with the long-awaited v3.2 release made publically available on February 28, 2020. A growing community of developers is actively contributing to its [GitHub project][3], while an equally thriving community of composers shares ideas and support in the [official forums][4]. The project is now also financially assisted through a [Patreon campaign][5], and Sam himself has been spreading the word of Sonic Pi through schools, conferences, and workshops worldwide.
What really shines about Sonic Pi is its approachability. Releases are available for many major flavors of OS, including Windows, macOS, Linux, and of course, the Raspberry Pi itself. In fact, getting started with Sonic Pi on a Raspberry Pi couldn't be simpler; it comes pre-installed with [Raspbian][6], so if you have an existing Raspbian-based setup, you'll find it situated in the programming menu.
Upon loading Sonic Pi for the first time, you'll be greeted with a simple interface with two main areas: an editor in which to write your code, and a section devoted to Sonic Pi's expansive tutorial. For newcomers, the tutorial is an essential resource for learning the basics, featuring accompanying music programs to reinforce each concept being taught.
If you're following along, let's code ourselves a simple bit of music and explore the potential of live-coding music. Type or paste the following code into the Sonic Pi editor:
```
live_loop :beat do
  sample :drum_heavy_kick
  sleep 1
end
```
Even if you're a Sonic Pi novice, many coders may immediately understand what's going on here. We're playing a drum kick sample, sleeping for a second, and then repeating. Click the Run button or press ALT+R (meta+R on macOS), and you should hear it begin to play.
This isn't a very exciting song yet, so let's liven it up with a snare playing on the off-beat. Replace the existing code with the block below and Run again. You can leave the existing beat playing while you do this; you'll notice that your changes will be applied naturally, in time with the beat:
```
live_loop :beat do
  sample :drum_heavy_kick
  sleep 0.5
  sample :drum_snare_soft
  sleep 0.5
end
```
While we're at it, let's add a hi-hat right before every fourth beat, just to make things a little interesting. Add this new block below our existing one and Run again:
```
live_loop :hihat do
  sleep 3.9
  sample :drum_cymbal_closed
  sleep 0.1
end
```
We've got our beat going now, so let's add a bassline! Sonic Pi comes with a variety of synths built-in, along with effects filters such as reverb and distortion. We'll use a combination of the "dsaw" and "tech_saw" synths to give it an electronic retro-synth feel. Add the block below to your existing program, Run, and have a listen:
```
live_loop :bass do
  use_synth :dsaw
  play :a2, attack: 1, release: 2, amp: 0.3
  sleep 2.5
  use_synth :tech_saws
  play :a1, attack: 1, release: 1.5, amp: 0.8
  sleep 1.5
end
```
You'll note above that we have full control over the [ADSR][7] envelope when playing notes, so we can decide when each sound should peak and fade.
Lastly, let's add a lead synth and try out one of those effects features known as the "slicer." To spice things up, we'll also introduce an element of pseudo-randomness by letting Sonic Pi pick from a series of potential chords. This is where some of the fun improvisation and "happy accidents" can begin to occur. Add the block below to your existing program and Run:
```
live_loop :lead do
  with_fx :slicer do
    chords = [(chord :A4, :minor7), (chord :A4, :minor), (chord :D4, :minor7), (chord :F4, :major7)]
    use_synth :blade
    play chords.choose, attack: 1, release: 2, amp: 1
    sleep 2
  end
end
```
Great! Now, we're certainly not going to be competing with Daft Punk any time soon, but hopefully, through this process, you've seen how we can go from a bare beat to something much bigger, in real-time, by adding some simple morsels of code. It is well worth watching one of Sam Aaron's [live coding performances][8] on YouTube for a demonstration of how creative and adaptive Sonic Pi can let you be.
![Sonic Pi composition example][9]
Our finished piece, in full
If you've ever wanted to learn a musical instrument, but felt held back by thoughts like "I don't have rhythm" or "my hands aren't nimble enough," Sonic Pi is a versatile instrument for which none of those things matter. All you need are the ideas, the inspiration, and an inexpensive computer such as the humble Raspberry Pi. The rest is at your fingertips—literally!
Here are a few handy links to get you started:
* The Official Sonic Pi [website][10] and [tutorial][11]
* [Getting Started with Sonic Pi][12] ([projects.raspberrypi.org][13])
* Sonic Pi [Github project][3]
--------------------------------------------------------------------------------
via: https://opensource.com/article/20/3/sonic-pi
作者:[Matt Bargenquast][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mbargenquast
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/music-birds-recording-520.png?itok=UoM7brl0 (Bird singing and music notes)
[2]: https://opensource.com/life/15/10/interview-sam-aaron-sonic-pi
[3]: https://github.com/samaaron/sonic-pi/
[4]: https://in-thread.sonic-pi.net/
[5]: https://www.patreon.com/samaaron
[6]: https://www.raspberrypi.org/downloads/raspbian/
[7]: https://en.wikipedia.org/wiki/Envelope_(music)
[8]: https://www.youtube.com/watch?v=JEHpS1aTKp0
[9]: https://opensource.com/sites/default/files/uploads/sonicpi.png (Sonic Pi composition example)
[10]: https://sonic-pi.net/
[11]: https://sonic-pi.net/tutorial.html
[12]: https://projects.raspberrypi.org/en/projects/getting-started-with-sonic-pi
[13]: http://projects.raspberrypi.org