问:
SwiftyJSON/Alamofire not parsing string to UTF8

Introduction

Hey there! In my app I am making requests to the YouTubeDataAPI. The api is capable of responding with UTF8-Encoded Strings (special characters included). However, I am unable to receive the data as utf8 data.

In order to parse the responded data into an Object, I am using Swift's codable protocol.

This is what my request looks like

```<code>enum VideoPart: String {
    case snippet = "snippet"
    case statistics = "statistics"
    case contentDetails = "contentDetails"
}

private static func fetchDetailsAfterSearch(forVideo videoId: String, parts: [VideoPart], onDone: @escaping (JSON) -> Void) {
        let videoParts = parts.map({ $0.rawValue })

        let apiUrl = URL(string: "https://www.googleapis.com/youtube/v3/videos")

        let headers: HTTPHeaders = ["X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier ?? ""]

        let parameters: Parameters = ["part": videoParts.joined(separator: ","), "id": videoId, "key": apiKey]

        Alamofire.request(apiUrl!, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { (response) in
            if let responseData = response.data {
                onDone(JSON(responseData))
            }
        }
    }

static func searchVideos(forQuery query: String, limit: Int = 20, onDone: @escaping ([YTVideo]) -> Void) {

    let apiUrl = URL(string: "https://www.googleapis.com/youtube/v3/search")!

    let headers: HTTPHeaders = ["X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier ?? ""]

    let parameters: Parameters = ["q": query, "part": "snippet", "maxResults": limit, "relevanceLanguage": "en", "type": "video", "key": apiKey]

    let group = DispatchGroup()
    group.enter()

    var videos: [YTVideo] = [] // the parsed videos are stored here

    Alamofire.request(apiUrl, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { (response) in

        if let responseData = response.data { // is there a response data?
            let resultVideos = JSON(responseData)["items"].arrayValue

            resultVideos.forEach({ (v) in // loop through each video and fetch more exact data, based on the videoId
                let videoId = v["id"]["videoId"].stringValue
                group.enter()
                YTDataService.fetchDetailsAfterSearch(forVideo: videoId, parts: [VideoPart.statistics, VideoPart.contentDetails], onDone: {(details) in
                    // MARK: parse the data of the api to the YTVideo Object
                    let videoSnippet = v["snippet"]
                    let videoDetails = details["items"][0]

                    var finalJSON: JSON = JSON()

                    finalJSON = finalJSON.merged(other: videoSnippet)
                    finalJSON = finalJSON.merged(other: videoDetails)

                    if let video = try? YTVideo(data: finalJSON.rawData()) {
                        videos.append(video)
                    }
                    group.leave()
                })
            })
            group.leave()
        }
    }

    group.notify(queue: .main) {
        onDone(videos)
    }
}
</code>```

Code explanation:

As the api only returns the snippet of the video, I have to make another api request for each video to fetch more details. This request is made inside a for-loop for each video. This call then returns a Data Object that is parsed to a JSON Object (by SwiftyJSON).

Those two responses are then merged together into one JSON object. After that, the finalJson is used to initialize a YTVideo Object. As I already said, the class is codable and parses the json automatically to its needs - the structure of the class can be found below.

The data that is sent back from the API:

```<code>{
  "statistics" : {
    "favoriteCount" : "0",
    "dislikeCount" : "942232",
    "likeCount" : "8621179",
    "commentCount" : "516305",
    "viewCount" : "2816892915"
  },
  "publishedAt" : "2014-08-18T21:18:00.000Z",
  "contentDetails" : {
    "caption" : "false",
    "licensedContent" : true,
    "definition" : "hd",
    "duration" : "PT4M2S",
    "dimension" : "2d",
    "projection" : "rectangular"
  },
  "channelId" : "UCANLZYMidaCbLQFWXBC95Jg",
  "kind" : "youtube#video",
  "id" : "nfWlot6h_JM",
  "liveBroadcastContent" : "none",
  "etag" : "\"8jEFfXBrqiSrcF6Ee7MQuz8XuAM\/ChcYFUcK77KQsdMIp5DyWCHvX9I\"",
  "title" : "Taylor Swift - Shake It Off",
  "channelTitle" : "TaylorSwiftVEVO",
  "description" : "Music video by Taylor Swift performing Shake It Off. (C) 2014 Big Machine Records, LLC. New single ME! (feat. Brendon Urie of Panic! At The Disco) available ...",
  "thumbnails" : {
    "high" : {
      "width" : 480,
      "url" : "https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/hqdefault.jpg",
      "height" : 360
    },
    "medium" : {
      "url" : "https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/mqdefault.jpg",
      "width" : 320,
      "height" : 180
    },
    "default" : {
      "url" : "https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/default.jpg",
      "width" : 120,
      "height" : 90
    }
  }
}
</code>```

This is my YTVideo class

```<code>// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let yTVideo = try YTVideo(json)

import Foundation

// MARK: - YTVideo
struct YTVideo: Codable {
    let statistics: Statistics
    let publishedAt: String
    let contentDetails: ContentDetails
    let channelID, kind, id, liveBroadcastContent: String
    let etag, title, channelTitle, ytVideoDescription: String
    let thumbnails: Thumbnails

    enum CodingKeys: String, CodingKey {
        case statistics, publishedAt, contentDetails
        case channelID = "channelId"
        case kind, id, liveBroadcastContent, etag, title, channelTitle
        case ytVideoDescription = "description"
        case thumbnails
    }
}

// MARK: YTVideo convenience initializers and mutators

extension YTVideo {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(YTVideo.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        statistics: Statistics? = nil,
        publishedAt: String? = nil,
        contentDetails: ContentDetails? = nil,
        channelID: String? = nil,
        kind: String? = nil,
        id: String? = nil,
        liveBroadcastContent: String? = nil,
        etag: String? = nil,
        title: String? = nil,
        channelTitle: String? = nil,
        ytVideoDescription: String? = nil,
        thumbnails: Thumbnails? = nil
    ) -> YTVideo {
        return YTVideo(
            statistics: statistics ?? self.statistics,
            publishedAt: publishedAt ?? self.publishedAt,
            contentDetails: contentDetails ?? self.contentDetails,
            channelID: channelID ?? self.channelID,
            kind: kind ?? self.kind,
            id: id ?? self.id,
            liveBroadcastContent: liveBroadcastContent ?? self.liveBroadcastContent,
            etag: etag ?? self.etag,
            title: title ?? self.title,
            channelTitle: channelTitle ?? self.channelTitle,
            ytVideoDescription: ytVideoDescription ?? self.ytVideoDescription,
            thumbnails: thumbnails ?? self.thumbnails
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - ContentDetails
struct ContentDetails: Codable {
    let caption: String
    let licensedContent: Bool
    let definition, duration, dimension, projection: String
}

// MARK: ContentDetails convenience initializers and mutators

extension ContentDetails {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(ContentDetails.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        caption: String? = nil,
        licensedContent: Bool? = nil,
        definition: String? = nil,
        duration: String? = nil,
        dimension: String? = nil,
        projection: String? = nil
    ) -> ContentDetails {
        return ContentDetails(
            caption: caption ?? self.caption,
            licensedContent: licensedContent ?? self.licensedContent,
            definition: definition ?? self.definition,
            duration: duration ?? self.duration,
            dimension: dimension ?? self.dimension,
            projection: projection ?? self.projection
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Statistics
struct Statistics: Codable {
    let favoriteCount, dislikeCount, likeCount, commentCount: String
    let viewCount: String
}

// MARK: Statistics convenience initializers and mutators

extension Statistics {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Statistics.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        favoriteCount: String? = nil,
        dislikeCount: String? = nil,
        likeCount: String? = nil,
        commentCount: String? = nil,
        viewCount: String? = nil
    ) -> Statistics {
        return Statistics(
            favoriteCount: favoriteCount ?? self.favoriteCount,
            dislikeCount: dislikeCount ?? self.dislikeCount,
            likeCount: likeCount ?? self.likeCount,
            commentCount: commentCount ?? self.commentCount,
            viewCount: viewCount ?? self.viewCount
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Thumbnails
struct Thumbnails: Codable {
    let high, medium, thumbnailsDefault: Default

    enum CodingKeys: String, CodingKey {
        case high, medium
        case thumbnailsDefault = "default"
    }
}

// MARK: Thumbnails convenience initializers and mutators

extension Thumbnails {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Thumbnails.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        high: Default? = nil,
        medium: Default? = nil,
        thumbnailsDefault: Default? = nil
    ) -> Thumbnails {
        return Thumbnails(
            high: high ?? self.high,
            medium: medium ?? self.medium,
            thumbnailsDefault: thumbnailsDefault ?? self.thumbnailsDefault
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Default
struct Default: Codable {
    let width: Int
    let url: String
    let height: Int
}

// MARK: Default convenience initializers and mutators

extension Default {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Default.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        width: Int? = nil,
        url: String? = nil,
        height: Int? = nil
    ) -> Default {
        return Default(
            width: width ?? self.width,
            url: url ?? self.url,
            height: height ?? self.height
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Helper functions for creating encoders and decoders

func newJSONDecoder() -> JSONDecoder {
    let decoder = JSONDecoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        decoder.dateDecodingStrategy = .iso8601
    }
    return decoder
}

func newJSONEncoder() -> JSONEncoder {
    let encoder = JSONEncoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        encoder.dateEncodingStrategy = .iso8601
    }
    return encoder
}
</code>```

What I currently have:

The parsing and everything is working fine, however the Youtube-Video-Title is not displayed in utf8 (see image below).

What I want

What changes do I have to make in order to display the data from the YouTube API as a valid utf8 encoded string? I tried several utf8-encodings but none of them worked for me:

Solution 1

Solution 2

Solution 3

Any help would be appreciated!

答:
Hope this help you:

```<code>extension String {
func htmlToUtf8() -> String{
    //chuyển đổi kết quả từ JSON htmlString sang Utf8
    let encodedData = self.data(using: .utf8)
    let attributedOptions : [NSAttributedString.DocumentReadingOptionKey : Any ] = [
        .documentType: NSAttributedString.DocumentType.html,
        .characterEncoding: String.Encoding.utf8.rawValue ]
    do {
        let attributedString = try NSAttributedString(data: encodedData!, options: attributedOptions, documentAttributes: nil)
        let decodedString = attributedString.string
        return decodedString
    } catch {
        // error ...
    }

    return String()
}
</code>```

}

And then:

```<code>let jsonTitle = "ERIK - 'Em Kh\U00f4ng Sai, Ch\U00fang Ta Sai' (Official Lyric Video)"
let videoTitle = jsonTitle.htmlToUtf8()
print(videoTitle) //"ERIK - 'Em Không Sai, Chúng Ta Sai' (Official Lyric Video)"
</code>```

I'm from VietNam, so we use utf8 a lot.

Body:
Introduction

Hey there! In my app I am making requests to the YouTubeDataAPI. The api is capable of responding with UTF8-Encoded Strings (special characters included). However, I am unable to receive the data as utf8 data.

In order to parse the responded data into an Object, I am using Swift's codable protocol.

This is what my request looks like

enum VideoPart: String {
    case snippet = "snippet"
    case statistics = "statistics"
    case contentDetails = "contentDetails"
}

private static func fetchDetailsAfterSearch(forVideo videoId: String, parts: [VideoPart], onDone: @escaping (JSON) -> Void) {
        let videoParts = parts.map({ $0.rawValue })

        let apiUrl = URL(string: "https://www.googleapis.com/youtube/v3/videos")

        let headers: HTTPHeaders = ["X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier ?? ""]

        let parameters: Parameters = ["part": videoParts.joined(separator: ","), "id": videoId, "key": apiKey]

        Alamofire.request(apiUrl!, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { (response) in
            if let responseData = response.data {
                onDone(JSON(responseData))
            }
        }
    }

static func searchVideos(forQuery query: String, limit: Int = 20, onDone: @escaping ([YTVideo]) -> Void) {

    let apiUrl = URL(string: "https://www.googleapis.com/youtube/v3/search")!

    let headers: HTTPHeaders = ["X-Ios-Bundle-Identifier": Bundle.main.bundleIdentifier ?? ""]

    let parameters: Parameters = ["q": query, "part": "snippet", "maxResults": limit, "relevanceLanguage": "en", "type": "video", "key": apiKey]

    let group = DispatchGroup()
    group.enter()

    var videos: [YTVideo] = [] // the parsed videos are stored here

    Alamofire.request(apiUrl, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { (response) in

        if let responseData = response.data { // is there a response data?
            let resultVideos = JSON(responseData)["items"].arrayValue

            resultVideos.forEach({ (v) in // loop through each video and fetch more exact data, based on the videoId
                let videoId = v["id"]["videoId"].stringValue
                group.enter()
                YTDataService.fetchDetailsAfterSearch(forVideo: videoId, parts: [VideoPart.statistics, VideoPart.contentDetails], onDone: {(details) in
                    // MARK: parse the data of the api to the YTVideo Object
                    let videoSnippet = v["snippet"]
                    let videoDetails = details["items"][0]

                    var finalJSON: JSON = JSON()

                    finalJSON = finalJSON.merged(other: videoSnippet)
                    finalJSON = finalJSON.merged(other: videoDetails)

                    if let video = try? YTVideo(data: finalJSON.rawData()) {
                        videos.append(video)
                    }
                    group.leave()
                })
            })
            group.leave()
        }
    }

    group.notify(queue: .main) {
        onDone(videos)
    }
}

Code explanation:

As the api only returns the snippet of the video, I have to make another api request for each video to fetch more details. This request is made inside a for-loop for each video. This call then returns a Data Object that is parsed to a JSON Object (by SwiftyJSON).

Those two responses are then merged together into one JSON object. After that, the finalJson is used to initialize a YTVideo Object. As I already said, the class is codable and parses the json automatically to its needs - the structure of the class can be found below.

The data that is sent back from the API:

{
  "statistics" : {
    "favoriteCount" : "0",
    "dislikeCount" : "942232",
    "likeCount" : "8621179",
    "commentCount" : "516305",
    "viewCount" : "2816892915"
  },
  "publishedAt" : "2014-08-18T21:18:00.000Z",
  "contentDetails" : {
    "caption" : "false",
    "licensedContent" : true,
    "definition" : "hd",
    "duration" : "PT4M2S",
    "dimension" : "2d",
    "projection" : "rectangular"
  },
  "channelId" : "UCANLZYMidaCbLQFWXBC95Jg",
  "kind" : "youtube#video",
  "id" : "nfWlot6h_JM",
  "liveBroadcastContent" : "none",
  "etag" : "\"8jEFfXBrqiSrcF6Ee7MQuz8XuAM\/ChcYFUcK77KQsdMIp5DyWCHvX9I\"",
  "title" : "Taylor Swift - Shake It Off",
  "channelTitle" : "TaylorSwiftVEVO",
  "description" : "Music video by Taylor Swift performing Shake It Off. (C) 2014 Big Machine Records, LLC. New single ME! (feat. Brendon Urie of Panic! At The Disco) available ...",
  "thumbnails" : {
    "high" : {
      "width" : 480,
      "url" : "https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/hqdefault.jpg",
      "height" : 360
    },
    "medium" : {
      "url" : "https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/mqdefault.jpg",
      "width" : 320,
      "height" : 180
    },
    "default" : {
      "url" : "https:\/\/i.ytimg.com\/vi\/nfWlot6h_JM\/default.jpg",
      "width" : 120,
      "height" : 90
    }
  }
}

This is my YTVideo class

// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse the JSON, add this file to your project and do:
//
//   let yTVideo = try YTVideo(json)

import Foundation

// MARK: - YTVideo
struct YTVideo: Codable {
    let statistics: Statistics
    let publishedAt: String
    let contentDetails: ContentDetails
    let channelID, kind, id, liveBroadcastContent: String
    let etag, title, channelTitle, ytVideoDescription: String
    let thumbnails: Thumbnails

    enum CodingKeys: String, CodingKey {
        case statistics, publishedAt, contentDetails
        case channelID = "channelId"
        case kind, id, liveBroadcastContent, etag, title, channelTitle
        case ytVideoDescription = "description"
        case thumbnails
    }
}

// MARK: YTVideo convenience initializers and mutators

extension YTVideo {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(YTVideo.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        statistics: Statistics? = nil,
        publishedAt: String? = nil,
        contentDetails: ContentDetails? = nil,
        channelID: String? = nil,
        kind: String? = nil,
        id: String? = nil,
        liveBroadcastContent: String? = nil,
        etag: String? = nil,
        title: String? = nil,
        channelTitle: String? = nil,
        ytVideoDescription: String? = nil,
        thumbnails: Thumbnails? = nil
    ) -> YTVideo {
        return YTVideo(
            statistics: statistics ?? self.statistics,
            publishedAt: publishedAt ?? self.publishedAt,
            contentDetails: contentDetails ?? self.contentDetails,
            channelID: channelID ?? self.channelID,
            kind: kind ?? self.kind,
            id: id ?? self.id,
            liveBroadcastContent: liveBroadcastContent ?? self.liveBroadcastContent,
            etag: etag ?? self.etag,
            title: title ?? self.title,
            channelTitle: channelTitle ?? self.channelTitle,
            ytVideoDescription: ytVideoDescription ?? self.ytVideoDescription,
            thumbnails: thumbnails ?? self.thumbnails
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - ContentDetails
struct ContentDetails: Codable {
    let caption: String
    let licensedContent: Bool
    let definition, duration, dimension, projection: String
}

// MARK: ContentDetails convenience initializers and mutators

extension ContentDetails {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(ContentDetails.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        caption: String? = nil,
        licensedContent: Bool? = nil,
        definition: String? = nil,
        duration: String? = nil,
        dimension: String? = nil,
        projection: String? = nil
    ) -> ContentDetails {
        return ContentDetails(
            caption: caption ?? self.caption,
            licensedContent: licensedContent ?? self.licensedContent,
            definition: definition ?? self.definition,
            duration: duration ?? self.duration,
            dimension: dimension ?? self.dimension,
            projection: projection ?? self.projection
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Statistics
struct Statistics: Codable {
    let favoriteCount, dislikeCount, likeCount, commentCount: String
    let viewCount: String
}

// MARK: Statistics convenience initializers and mutators

extension Statistics {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Statistics.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        favoriteCount: String? = nil,
        dislikeCount: String? = nil,
        likeCount: String? = nil,
        commentCount: String? = nil,
        viewCount: String? = nil
    ) -> Statistics {
        return Statistics(
            favoriteCount: favoriteCount ?? self.favoriteCount,
            dislikeCount: dislikeCount ?? self.dislikeCount,
            likeCount: likeCount ?? self.likeCount,
            commentCount: commentCount ?? self.commentCount,
            viewCount: viewCount ?? self.viewCount
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Thumbnails
struct Thumbnails: Codable {
    let high, medium, thumbnailsDefault: Default

    enum CodingKeys: String, CodingKey {
        case high, medium
        case thumbnailsDefault = "default"
    }
}

// MARK: Thumbnails convenience initializers and mutators

extension Thumbnails {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Thumbnails.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        high: Default? = nil,
        medium: Default? = nil,
        thumbnailsDefault: Default? = nil
    ) -> Thumbnails {
        return Thumbnails(
            high: high ?? self.high,
            medium: medium ?? self.medium,
            thumbnailsDefault: thumbnailsDefault ?? self.thumbnailsDefault
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Default
struct Default: Codable {
    let width: Int
    let url: String
    let height: Int
}

// MARK: Default convenience initializers and mutators

extension Default {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(Default.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        width: Int? = nil,
        url: String? = nil,
        height: Int? = nil
    ) -> Default {
        return Default(
            width: width ?? self.width,
            url: url ?? self.url,
            height: height ?? self.height
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}

// MARK: - Helper functions for creating encoders and decoders

func newJSONDecoder() -> JSONDecoder {
    let decoder = JSONDecoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        decoder.dateDecodingStrategy = .iso8601
    }
    return decoder
}

func newJSONEncoder() -> JSONEncoder {
    let encoder = JSONEncoder()
    if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
        encoder.dateEncodingStrategy = .iso8601
    }
    return encoder
}

What I currently have:

The parsing and everything is working fine, however the Youtube-Video-Title is not displayed in utf8 (see image below).

What I want

What changes do I have to make in order to display the data from the YouTube API as a valid utf8 encoded string? I tried several utf8-encodings but none of them worked for me:

Solution 1

Solution 2

Solution 3

Any help would be appreciated!

Body:
Hope this help you:

extension String {
func htmlToUtf8() -> String{
    //chuyển đổi kết quả từ JSON htmlString sang Utf8
    let encodedData = self.data(using: .utf8)
    let attributedOptions : [NSAttributedString.DocumentReadingOptionKey : Any ] = [
        .documentType: NSAttributedString.DocumentType.html,
        .characterEncoding: String.Encoding.utf8.rawValue ]
    do {
        let attributedString = try NSAttributedString(data: encodedData!, options: attributedOptions, documentAttributes: nil)
        let decodedString = attributedString.string
        return decodedString
    } catch {
        // error ...
    }

    return String()
}

}

And then:

let jsonTitle = "ERIK - &#39;Em Kh\U00f4ng Sai, Ch\U00fang Ta Sai&#39; (Official Lyric Video)"
let videoTitle = jsonTitle.htmlToUtf8()
print(videoTitle) //"ERIK - 'Em Không Sai, Chúng Ta Sai' (Official Lyric Video)"

I'm from VietNam, so we use utf8 a lot.


来源: stackexchange

问:
FinViz - Stock scraping giving error --AMZN not found 'NoneType' object has no attribute 'find_next'
I am new to BeautifulSoup package.
I am playing with a code that I got from some website and I got stuck with the above error. Please help.</p>

```<code>import pandas as pd
import re
from bs4 import BeautifulSoup as bs
import requests

def get_fundamental_data(df):
    for symbol in df.index:
        try:
            url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())
            soup = bs(requests.get(url).content)
            for m in df.columns:
                df.loc[symbol,m] = fundamental_metric(soup,m)
        except Exception as e:
            print (symbol, 'not found')
            print(e)
    return df

def fundamental_metric(soup, metric):
    return soup.find(text=metric).find_next(class_='snapshot-td2').text

# Define A List Of Stocks And The Fundamental Metrics

stock_list = ['AMZN', 'GOOG', 'PG', 'KO', 'IBM', 'DG', 'XOM',
              'KO', 'PEP', 'MT', 'NL', 'ALDW', 'DCM', 'GSB', 'LPL']
metric = ['P/B',
          'P/E',
          'Forward P/E'
          ]

df = pd.DataFrame(index=stock_list, columns=metric)
df = get_fundamental_data(df)
df.head()
</code>```

答:
I ran your code and when you print(soup) in the for loop, you get this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>403 - Forbidden: Access is denied.</title>

Server Error

<fieldset>

403 - Forbidden: Access is denied.

You do not have permission to view this directory or page using the credentials that you supplied.

</fieldset>

```

ContentLicense: CC BY-SA 4.0

Body:
I am new to BeautifulSoup package.
I am playing with a code that I got from some website and I got stuck with the above error. Please help.

import pandas as pd
import re
from bs4 import BeautifulSoup as bs
import requests

def get_fundamental_data(df):
    for symbol in df.index:
        try:
            url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())
            soup = bs(requests.get(url).content)
            for m in df.columns:
                df.loc[symbol,m] = fundamental_metric(soup,m)
        except Exception as e:
            print (symbol, 'not found')
            print(e)
    return df

def fundamental_metric(soup, metric):
    return soup.find(text=metric).find_next(class_='snapshot-td2').text

# Define A List Of Stocks And The Fundamental Metrics

stock_list = ['AMZN', 'GOOG', 'PG', 'KO', 'IBM', 'DG', 'XOM',
              'KO', 'PEP', 'MT', 'NL', 'ALDW', 'DCM', 'GSB', 'LPL']
metric = ['P/B',
          'P/E',
          'Forward P/E'
          ]

df = pd.DataFrame(index=stock_list, columns=metric)
df = get_fundamental_data(df)
df.head()

Body:
I ran your code and when you print(soup) in the for loop, you get this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>403 - Forbidden: Access is denied.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>403 - Forbidden: Access is denied.</h2>
<h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
</fieldset></div>
</div>
</body>
</html>


问:
how do I format long config file lines
my frontend section has a huge number of 'certs'. The entire line is about 320-400 bytes. The normal bash or shell line continuation does not work...

```<code>frontend haproxy_www
        bind *:80
        bind *:443 ssl crt cert1.pem crt cert2.pem crt cert3.pem crt cert4.pem
</code>```

I tried (which did not work):

```<code>frontend haproxy_www
        bind *:80
        bind *:443 ssl \
crt cert1.pem \
crt cert2.pem \
crt cert3.pem \
crt cert4.pem
</code>```

PS I read the doc and at the top of the doc where it describes the document format it does not mention long line continuation or anything close.

答: In an slightly related question... asking about joining PEM files...  but one recommendation that seems to be ok is concat the individual PEM files into one. That'll do it.</p>

Title: how do I format long config file lines

Body:
my frontend section has a huge number of 'certs'. The entire line is about 320-400 bytes. The normal bash or shell line continuation does not work...

frontend haproxy_www
        bind *:80
        bind *:443 ssl crt cert1.pem crt cert2.pem crt cert3.pem crt cert4.pem

I tried (which did not work):

frontend haproxy_www
        bind *:80
        bind *:443 ssl \
crt cert1.pem \
crt cert2.pem \
crt cert3.pem \
crt cert4.pem

PS I read the doc and at the top of the doc where it describes the document format it does not mention long line continuation or anything close.


问:
How to create a single add method for different entities?
How can I create a single add method that will insert different entities in the jdbc?  I want to use annotations and reflection for that.

I have created 2 annotations:

```<code>(...)
public @interface Column {
    String name();
    boolean isPrimaryKey() default false;
}
</code>```

and

```<code>(...)
public @interface Table {
    String name();
}
</code>```

Suppose we have 2 entities/models/etc. : Client and Waiter. For both we should make 2 add methods, each with own INSERT.

If we have like 4 entities, we should have 4 add methods for each entity. Instead of having 4 add methods, how can I make just 1 add method? and by using the annotations and the reflection.

Eg:

```<code>@Table(name = "Table_Client")
public class Client{
    @Column(name = "ID", isPrimaryKey = true)
    private long id;

    @Column(name = "FULL_NAME")
    private String name;
}

@Table(name = "Table_Waiter")
public class Waiter {

    @Column(name = "FULL_NAME", isPrimaryKey = true)
    private String name;

    @Column(name = "AGE")
    private int age;
}

case: db.add(Client c1) => add to the database in the table Table_Client the client c1
      db.add(Waiter w1) => add to the database in the table Table_Waiter the waiter w1
      and so on...
</code>```

My idea is to take the class of the given object and scan it for the TYPE annotation to get the table's name. Then, take all the field's annotations and make a dynamic INSERT INTO VALUES query, but the problem is that I can't actually do that, because I can't pass the object's arguments.

Another question: if this can be done, update and delete methods can follow the same path?

答:
I cannot refrain from mentioning how many holes you may find in the road ahead. But judging from the comments, that's the path you want to explore.

First of all, regarding your existing code, you need to apply a retention meta-annotation to your annotations, Column and Table. For example:

@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    String name();
    boolean isPrimaryKey() default false;
}
```

<p>This meta-annotation will ensure that you can read your annotations at runtime through reflection.</p>

<p>Then, you need to inspect the class searching for these annotations at both class and field levels.</p>

<p>The <code>Class</code> class will have everything you need. You should know you can get it from any object by calling the <code>getClass</code> method. It contains a couple of important methods for what you are trying to achieve:</p>

<ul>
<li><code>getAnnotation(Class c)</code> will return the annotation if it exists, or null otherwise.</li>
<li><code>getDeclaredFields</code> will return all declared class fields, even private ones.</li>
</ul>

<p>At a field level, the Field class provides the following methods:</p>

<ul>
<li><code>getAnnotation(Class c)</code>, same as above, will return the annotation if it exists, or null otherwise.</li>
<li><code>getType</code> will return the class associated with the field</li>
</ul>

<p>Now consider the following piece of code:</p>

<pre class="lang-java prettyprint-override"><code>public static void inspectClass(Class<?> cls) {
    Table t = cls.getAnnotation(Table.class);

    if (t != null) {
        System.out.print(t.name() + " --> ");
        for (Field f: cls.getDeclaredFields()) {
            Column c = f.getAnnotation(Column.class);
            if (c != null) {
                System.out.print(c.name()
                        + " "
                        + f.getType().getSimpleName()
                        + (c.isPrimaryKey() ? " PK" : "") + ", ");
            }
        }
    }
}
</code>```

Applying this to your Client class, for instance, would return something like:

Table_Client --> ID long PK, FULL_NAME String,
```

<p>Of course, this needs some work, but the idea is there.</p>

<p><strong>EDIT:</strong></p>

<p>To access values of an instance through reflection at runtime, for creating a dynamic INSERT statement, that could be done by calling <code>get</code> method on the Field class. When dealing with private fields though, it's necessary to tweak the privacy mode first:</p>

<pre class="lang-java prettyprint-override"><code>f.setAccessible(true);
Object value = f.get(myInstance);
</code>```

Body:
How can I create a single add method that will insert different entities in the jdbc?  I want to use annotations and reflection for that.

I have created 2 annotations:

(...)
public @interface Column {
    String name();
    boolean isPrimaryKey() default false;
}

and

(...)
public @interface Table {
    String name();
}

Suppose we have 2 entities/models/etc. : Client and Waiter. For both we should make 2 add methods, each with own INSERT.

If we have like 4 entities, we should have 4 add methods for each entity. Instead of having 4 add methods, how can I make just 1 add method? and by using the annotations and the reflection.

Eg:

@Table(name = "Table_Client")
public class Client{
    @Column(name = "ID", isPrimaryKey = true)
    private long id;

    @Column(name = "FULL_NAME")
    private String name;
}

@Table(name = "Table_Waiter")
public class Waiter {

    @Column(name = "FULL_NAME", isPrimaryKey = true)
    private String name;

    @Column(name = "AGE")
    private int age;
}

case: db.add(Client c1) => add to the database in the table Table_Client the client c1
      db.add(Waiter w1) => add to the database in the table Table_Waiter the waiter w1
      and so on...

My idea is to take the class of the given object and scan it for the TYPE annotation to get the table's name. Then, take all the field's annotations and make a dynamic INSERT INTO VALUES query, but the problem is that I can't actually do that, because I can't pass the object's arguments.

Another question: if this can be done, update and delete methods can follow the same path?

Body:
I cannot refrain from mentioning how many holes you may find in the road ahead. But judging from the comments, that's the path you want to explore.

First of all, regarding your existing code, you need to apply a retention meta-annotation to your annotations, Column and Table. For example:

@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    String name();
    boolean isPrimaryKey() default false;
}

This meta-annotation will ensure that you can read your annotations at runtime through reflection.

Then, you need to inspect the class searching for these annotations at both class and field levels.

The Class class will have everything you need. You should know you can get it from any object by calling the getClass method. It contains a couple of important methods for what you are trying to achieve:

getAnnotation(Class c) will return the annotation if it exists, or null otherwise.

getDeclaredFields will return all declared class fields, even private ones.

At a field level, the Field class provides the following methods:

getAnnotation(Class c), same as above, will return the annotation if it exists, or null otherwise.

getType will return the class associated with the field

Now consider the following piece of code:

public static void inspectClass(Class<?> cls) {
    Table t = cls.getAnnotation(Table.class);

    if (t != null) {
        System.out.print(t.name() + " --> ");
        for (Field f: cls.getDeclaredFields()) {
            Column c = f.getAnnotation(Column.class);
            if (c != null) {
                System.out.print(c.name()
                        + " "
                        + f.getType().getSimpleName()
                        + (c.isPrimaryKey() ? " PK" : "") + ", ");
            }
        }
    }
}

Applying this to your Client class, for instance, would return something like:

Table_Client --> ID long PK, FULL_NAME String,

Of course, this needs some work, but the idea is there.

EDIT:

To access values of an instance through reflection at runtime, for creating a dynamic INSERT statement, that could be done by calling get method on the Field class. When dealing with private fields though, it's necessary to tweak the privacy mode first:

f.setAccessible(true);
Object value = f.get(myInstance);


问:
.htaccess remove www with GCP hosted site
I have a website hosted on GCP bucket and I use cloudflare for HTTPS.
How can I use .htaccess file to remove www. from all website pages and redirect them to HTTPS?

Basically, I want:

```<code>http://www.example.com/ redirect to https://example.com/
https://www.example.com/ redirect to https://example.com/

and any other pages like
https://www.example.com/about.html or http://www.example.com/about.html redirect to https://example.com/about.html
</code>```

I found the configuration for the .htaccess file that I need to add to the root directory of my website:

Catch all and redirect www to no-www (HTTPS):

```<code>RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]
</code>```

I replaced example.com with my domain name, put the file into the root directory of my GCP bucket and nothing happened. If I try to access my website by http://example.com I get DNS_PROBE_FINISHED_NXDOMAIN error. I tried to save .htaccess with ANSI and with UTF-8 with the same result.

How do I:

Remove www. from my website using .htaccess

Redirect users to https:// without www.

What encoding should I use for .htaccess? Does it matter?

Is this even possible with my GCP bucket hosted static website and cloudflare provided https://?

I tried setting up redirects through my domain registrar's control panel, but they don't support redirects with dns nameservers other than their own default ones.

答:
There could be further issues at play here (It has been a while since I've messed with Apache), but the unescaped periods in your regex for the condition will definitely cause issues. Try this:</p>

```<code>RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</code>```

Body:
I have a website hosted on GCP bucket and I use cloudflare for HTTPS.
How can I use .htaccess file to remove www. from all website pages and redirect them to HTTPS?

Basically, I want:

http://www.example.com/ redirect to https://example.com/
https://www.example.com/ redirect to https://example.com/

and any other pages like
https://www.example.com/about.html or http://www.example.com/about.html redirect to https://example.com/about.html

I found the configuration for the .htaccess file that I need to add to the root directory of my website:

Catch all and redirect www to no-www (HTTPS):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]

I replaced example.com with my domain name, put the file into the root directory of my GCP bucket and nothing happened. If I try to access my website by http://example.com I get DNS_PROBE_FINISHED_NXDOMAIN error. I tried to save .htaccess with ANSI and with UTF-8 with the same result.

How do I:

Remove www. from my website using .htaccess

Redirect users to https:// without www.

What encoding should I use for .htaccess? Does it matter?

Is this even possible with my GCP bucket hosted static website and cloudflare provided https://?

I tried setting up redirects through my domain registrar's control panel, but they don't support redirects with dns nameservers other than their own default ones.

Body:
There could be further issues at play here (It has been a while since I've messed with Apache), but the unescaped periods in your regex for the condition will definitely cause issues. Try this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]


问:
Updating a number from a for loop
```<code>var currentmix = document.getElementById('custommix').value.split(",");
        var additionalprice = 0;

        for (i = 0; i < currentmix.length; i++) {
            if (currentmix[i] != "") {
                var mixinpriceid = "mixinprice"+currentmix[i];
                additionalprice += parseFloat(document.getElementById(mixinpriceid).value));
            }
        }
</code>```

This code is not working :(

I know the expression
parseFloat(document.getElementById(mixinpriceid).value)
is giving me the right number on each iteration of the loop

But it is not affecting the variable "additionalprice" outside the for loop.  Any ways to do this???????

答:
There's a additional parentheses in this statement, please check</p>

```<code>additionalprice += parseFloat(document.getElementById(mixinpriceid).value));
</code>```

Title: Updating a number from a for loop

Body:
var currentmix = document.getElementById('custommix').value.split(",");
        var additionalprice = 0;

        for (i = 0; i < currentmix.length; i++) {
            if (currentmix[i] != "") {
                var mixinpriceid = "mixinprice"+currentmix[i];
                additionalprice += parseFloat(document.getElementById(mixinpriceid).value));
            }
        }

This code is not working :(

I know the expression
parseFloat(document.getElementById(mixinpriceid).value)
is giving me the right number on each iteration of the loop

But it is not affecting the variable "additionalprice" outside the for loop.  Any ways to do this???????

Body:
There's a additional parentheses in this statement, please check

additionalprice += parseFloat(document.getElementById(mixinpriceid).value));


问:
How to append PDF pages using PyPDF2
Is anybody has experience merging two page of PDF file into one using python lib PyPDF2.
When I try page1.mergePage(page2) it results with page2 overlayed page1. How to make it to add page2 to the bottom of the page1?

答:
Did it this way:

```<code>reader = PyPDF2.PdfFileReader(open("input.pdf",'rb'))

NUM_OF_PAGES = reader.getNumPages()

page0 = reader.getPage(0)
h = page0.mediaBox.getHeight()
w = page0.mediaBox.getWidth()

newpdf_page = PyPDF2.pdf.PageObject.createBlankPage(None, w, h*NUM_OF_PAGES)
for i in range(NUM_OF_PAGES):
    next_page = reader.getPage(i)
    newpdf_page.mergeScaledTranslatedPage(next_page, 1, 0, h*(NUM_OF_PAGES-i-1))

writer = PdfFileWriter()
writer.addPage(newpdf_page)

with open('output.pdf', 'wb') as f:
    writer.write(f)
</code>```

It works when every page has the same height and width. Otherwise, it needs some modifications.

Maybe Emile Bergeron solution is better. Didn't try it.

ContentLicense: CC BY-SA 3.0

Title: How to append PDF pages using PyPDF2

Body:
Did it this way:

reader = PyPDF2.PdfFileReader(open("input.pdf",'rb'))

NUM_OF_PAGES = reader.getNumPages()

page0 = reader.getPage(0)
h = page0.mediaBox.getHeight()
w = page0.mediaBox.getWidth()

newpdf_page = PyPDF2.pdf.PageObject.createBlankPage(None, w, h*NUM_OF_PAGES)
for i in range(NUM_OF_PAGES):
    next_page = reader.getPage(i)
    newpdf_page.mergeScaledTranslatedPage(next_page, 1, 0, h*(NUM_OF_PAGES-i-1))

writer = PdfFileWriter()
writer.addPage(newpdf_page)

with open('output.pdf', 'wb') as f:
    writer.write(f)

It works when every page has the same height and width. Otherwise, it needs some modifications.

Maybe Emile Bergeron solution is better. Didn't try it.


问:
Api gateway still pointing to the old lambda
I have an API gateway with websocket

  WebSocket:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      ProtocolType: WEBSOCKET
      RouteSelectionExpression: "$request.body.action"
      Name: !Sub ${Appname}-${Environment}

  WebSocketStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      StageName: !Ref Environment
      AutoDeploy: true
      ApiId: !Ref WebSocket
```

<p>before that, I had a function <code>onConnect</code> attached to the API, but since I discovered that it was not necessary, I have remove from my Cloudformation stack. However now my Websocket is not working anymore.</p>

<p>I have enabled Cloudwatch logs for API Gateway and I have discovered that the route <code>onConnect</code> is still pointing to the old lambda, which does not exist anymore</p>

```
2020-05-31T19:09:56.014+07:00
(nnnnnnnnnn=) WebSocket Request Route: [$connect]

2020-05-31T19:09:56.014+07:00
(nnnnnnn=) Client [UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36, SourceIp: 115.73.134.142] is connecting to WebSocket API

2020-05-31T19:09:56.014+07:00
(nnnnnn=) Endpoint request URI: https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:11111111111:function:api-development-onConnectWebsocket-GGGGGGGGG/invocations <-------------------- OLD LAMBDA

2020-05-31T19:09:56.030+07:00
(nnnnnnnn=) Execution failed due to configuration error: Invalid permissions on Lambda function

```

In console nothing is showed, the $connect route is removed, so what is going on here? Unfortunately I wasn't able to figure it out, because the actual function does not exist and in console is pointing to the new one.

Can someone help?

Thanks

答:
It sounds like you are deploying your changes with cloudformation, but within api gateway you need to create a deployment for the changes you've made to go live.

You can test this by using the console to deploy your api gateway stage. You can learn a little more about api gateway deployment in cloudformation here: https://currentlyunnamed-theclassic.blogspot.com/2018/12/mastering-cloudformation-for-api.html

Body:
I have an API gateway with websocket

  WebSocket:
    Type: AWS::ApiGatewayV2::Api
    Properties:
      ProtocolType: WEBSOCKET
      RouteSelectionExpression: "$request.body.action"
      Name: !Sub ${Appname}-${Environment}

  WebSocketStage:
    Type: AWS::ApiGatewayV2::Stage
    Properties:
      StageName: !Ref Environment
      AutoDeploy: true
      ApiId: !Ref WebSocket

before that, I had a function onConnect attached to the API, but since I discovered that it was not necessary, I have remove from my Cloudformation stack. However now my Websocket is not working anymore.

I have enabled Cloudwatch logs for API Gateway and I have discovered that the route onConnect is still pointing to the old lambda, which does not exist anymore

2020-05-31T19:09:56.014+07:00
(nnnnnnnnnn=) WebSocket Request Route: [$connect]

2020-05-31T19:09:56.014+07:00
(nnnnnnn=) Client [UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36, SourceIp: 115.73.134.142] is connecting to WebSocket API

2020-05-31T19:09:56.014+07:00
(nnnnnn=) Endpoint request URI: https://lambda.ap-southeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-southeast-1:11111111111:function:api-development-onConnectWebsocket-GGGGGGGGG/invocations <-------------------- OLD LAMBDA

2020-05-31T19:09:56.030+07:00
(nnnnnnnn=) Execution failed due to configuration error: Invalid permissions on Lambda function

In console nothing is showed, the $connect route is removed, so what is going on here? Unfortunately I wasn't able to figure it out, because the actual function does not exist and in console is pointing to the new one.

Can someone help?

Thanks


问:
Using polymorphic function as a parameter
I have a function exec which accepts 3 parameters and applies a function f passed as first argument to the other two - p1 and p2.

```<code>  def exec[T](f: (T, T) => Boolean, p1: T, p2: T) = f(p1, p2)
</code>```

Everything works fine if I declare in advance a function which will be passed as an argument.

Somehow compiler can infer types for arguments of eq or in other words it can figure out that whatever in this case is Int

```<code>  // declaring a function with type parameter (polymorphic method)
  def eq[whatever](p1: whatever, p2: whatever) = p1 == p2
  // using a declared function
  println(exec(eq, 10, 10))
</code>```

It also works fine if I explicitly specify Int as shown below

```<code>  // specifying type explicitly in function literal
  println(exec((p1: Int, p2: Int) => p1 == p2, 10, 10))
  // specifying type parameter
  println(exec[Int]((p1, p2) => p1 == p2, 10, 10))
</code>```

Question 1

Is it possible to get below working?

```<code>println(exec((p1, p2) => p1 == p2, 10, 10))
</code>```

For example, by using implicits, defining exec differently or using some other way making it possible for compiler to infer types of p1 and p2 so that it does not fail with missing parameter type.

So no explicit type hints or declared methods are used.

Question 2

How does compiler infer types for eq and why it works for expressions like p1 == p2 or p1 != p2 but fails for p1 >= p2 (error is value >= is not a member of type parameter whatever)?

答:
Question 1 Is it possible to get below working?

If you can rewrite exec like this:

```<code>def exec[T](p1: T, p2: T)(f: (T, T) => Boolean) = f(p1, p2)
</code>```

Then, the compiler will already know the input type of the function.

So, you will be able to call it like this:

```<code>println(exec(10, 10) { case (p1, p2) => p1 == p2 })
</code>```

This is a common idiom, to put plain parameters first and then a function on a single parameter group.

How does compiler infer types for eq and why it works for expressions like p1 == p2 or p1 != p2 but fails for p1 >= p2

Because, Scala has universal equality (which is one of the things people most criticize of the language, but it was necessary for Java interop).

So, you can always compare two objects of any class for equality.

That is because equality is defined in the Any superclass as follows.

```<code>class Any {
  def equals(other: Any): Boolean
}
</code>```

But, ordering is not universal.

If you want, you may want to write a generic function which works for any type as long as there is an order for such type, you may be interested in the Ordering - typeclass.

But that is topic for another question.

Body:
I have a function exec which accepts 3 parameters and applies a function f passed as first argument to the other two - p1 and p2.

  def exec[T](f: (T, T) => Boolean, p1: T, p2: T) = f(p1, p2)

Everything works fine if I declare in advance a function which will be passed as an argument.

Somehow compiler can infer types for arguments of eq or in other words it can figure out that whatever in this case is Int

  // declaring a function with type parameter (polymorphic method)
  def eq[whatever](p1: whatever, p2: whatever) = p1 == p2
  // using a declared function
  println(exec(eq, 10, 10))

It also works fine if I explicitly specify Int as shown below

  // specifying type explicitly in function literal
  println(exec((p1: Int, p2: Int) => p1 == p2, 10, 10))
  // specifying type parameter
  println(exec[Int]((p1, p2) => p1 == p2, 10, 10))

Question 1

Is it possible to get below working?

println(exec((p1, p2) => p1 == p2, 10, 10))

For example, by using implicits, defining exec differently or using some other way making it possible for compiler to infer types of p1 and p2 so that it does not fail with missing parameter type.

So no explicit type hints or declared methods are used.

Question 2

How does compiler infer types for eq and why it works for expressions like p1 == p2 or p1 != p2 but fails for p1 >= p2 (error is value >= is not a member of type parameter whatever)?

Body:
Question 1 Is it possible to get below working?

If you can rewrite exec like this:

def exec[T](p1: T, p2: T)(f: (T, T) => Boolean) = f(p1, p2)

Then, the compiler will already know the input type of the function.

So, you will be able to call it like this:

println(exec(10, 10) { case (p1, p2) => p1 == p2 })

This is a common idiom, to put plain parameters first and then a function on a single parameter group.

How does compiler infer types for eq and why it works for expressions like p1 == p2 or p1 != p2 but fails for p1 >= p2

Because, Scala has universal equality (which is one of the things people most criticize of the language, but it was necessary for Java interop).

So, you can always compare two objects of any class for equality.

That is because equality is defined in the Any superclass as follows.

class Any {
  def equals(other: Any): Boolean
}

But, ordering is not universal.

If you want, you may want to write a generic function which works for any type as long as there is an order for such type, you may be interested in the Ordering - typeclass.

But that is topic for another question.


问:
This page isn’t working localhost didn’t send any data
when i click on the hrefs the page takes time to redirect but it doesn't at the end, it gives a message that the page is not working.

i run this project on localhost and the links used to work before. bewlow is the code snippet for the navbar

```<code> <ul class="nav navbar-nav navbar-right">
    <!-- 6 -->
    <li id="home"><a href="/">HOME</a></li>
    <li id="about"><a href="/about">ABOUT US</a> </li>
    <li id="contact"><a href="/contact">CONTACT US</a>
    </li>
    <li id="contact"><a href="/composePost">+</a></li>
  </ul>
</code>```

and here is the code in my app.js

```<code>app.get('/', (req, res) => {
  res.render('home')
})

app.get('/about', (req, res) => {
  res.render('about')
})

app.get("/contact", (req, res) => {
  res.render("contact");
});

app.get('/composePost', (req, res) => {
  res.send('compose')
})
</code>```

答:
I do the same, and it works.
Have you enter the correct server port?
mine is: localhost:3000/somthing, I mean, is literally the same code

```<code>const app = express();

app.get('/about', (req, res) => {
    res.render('about');
});
</code>```

Maybe the problem, is that you view is in a folder, but you are not specifying it, the is would be:

```<code>app.get('/about', (req, res) => {
    res.render('folder/about');
});
</code>```

Maybe you are not definening the layout correctly.

```<code>app.set('view engine', 'ejs');
</code>```

Body:
when i click on the hrefs the page takes time to redirect but it doesn't at the end, it gives a message that the page is not working.

i run this project on localhost and the links used to work before. bewlow is the code snippet for the navbar

HOME

ABOUT US

CONTACT US

+

and here is the code in my app.js

app.get('/', (req, res) => {
  res.render('home')
})

app.get('/about', (req, res) => {
  res.render('about')
})

app.get("/contact", (req, res) => {
  res.render("contact");
});

app.get('/composePost', (req, res) => {
  res.send('compose')
})

Body:
I do the same, and it works.
Have you enter the correct server port?
mine is: localhost:3000/somthing, I mean, is literally the same code

const app = express();

app.get('/about', (req, res) => {
    res.render('about');
});

Maybe the problem, is that you view is in a folder, but you are not specifying it, the is would be:

app.get('/about', (req, res) => {
    res.render('folder/about');
});

Maybe you are not definening the layout correctly.

app.set('view engine', 'ejs');


问:
Why some IDEs (Intellij/Android Studio) has its vertical line as default?
Does anyone know the reason why the developers of some IDEs (IntelliJ IDEA/Android Studio) implement vertical line in the middle of the editor view by default.
Some of my friends told me it is frustrating to have this vertical line since he writes code on average exceeds the line. (oh by the way i love IntelliJ, no offense)

答:
Some coding standards enforce the maximum line length on the code and this indent guide helps you to understand where the code will be wrapped by the IDE formatter if you have configured the code style settings to wrap at the specified margin.

You can either change the indent margin in the IDE code style settings or disable it completely if you don't want to enforce any limits (set the hard wrap to 0):


问:
How To Have a Microsoft Report on Remote Server Network- Microsoft Report 2010 [rdlc]?
Hi
Really Iam very new to Microsoft Report. Before I used only Crystal report only. So I have no any idea.

Actaully from my system I used the below code and it works fine. But my program has to be accessed by 2 more users on network. On Network my program is working fine, no problem except report. For that I have no any idea, what else I need?
May be I need to install anything on my sqlserver installed machine in network?
or I have to install reportviewer to my users system ?
And any sample code for that?

Any Guidences will be happy.

Thanks For The Ideas.

Thanks & Regards
PARAMU

```
reportViewer1.RefreshReport();
Microsoft.Reporting.WinForms.ReportDataSource VehRDS = new Microsoft.Reporting.WinForms.ReportDataSource();
RepDS.Name = "DataSet1";
RepDS.Value = MyDataTable1;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(VehRDS);
reportViewer1.LocalReport.ReportPath ="C:\\Documents and Settings\\...\\My Documents\\Visual Studio 2010\\Projects\\MyProject\\My_Report.rdlc";
reportViewer1.RefreshReport();
reportViewer1.Visible = true;

```

答: Try this walkthrough: http://msdn.microsoft.com/en-us/library/ms251669.aspx</p>

Body:
Hi
Really Iam very new to Microsoft Report. Before I used only Crystal report only. So I have no any idea.

Actaully from my system I used the below code and it works fine. But my program has to be accessed by 2 more users on network. On Network my program is working fine, no problem except report. For that I have no any idea, what else I need?
May be I need to install anything on my sqlserver installed machine in network?
or I have to install reportviewer to my users system ?
And any sample code for that?

Any Guidences will be happy.

Thanks For The Ideas.

Thanks & Regards
PARAMU

reportViewer1.RefreshReport();
Microsoft.Reporting.WinForms.ReportDataSource VehRDS = new Microsoft.Reporting.WinForms.ReportDataSource();
RepDS.Name = "DataSet1";
RepDS.Value = MyDataTable1;
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(VehRDS);
reportViewer1.LocalReport.ReportPath ="C:\\Documents and Settings\\...\\My Documents\\Visual Studio 2010\\Projects\\MyProject\\My_Report.rdlc";
reportViewer1.RefreshReport();
reportViewer1.Visible = true;


问:
Is View already a pure component in react native?
In React Native, say I have something like

```<code>var styles = { ... }
export default class App extends React.Component {
    ...
    render() {
        return(
            <View style={styles.container}>
                <View style={{width: this.state.widthA}} />
                <View style={{width: this.state.widthB}} />
            </View>
        );
    }
}
</code>```

when the state widthA changes, the render() function is called again. Will the sibling View (widthB) also re-render? Would there be any performance boost to define

```<code>class PureView extends React.PureComponent {
    constructor(props) {
        super(props);
    }
    render() {
        return <View {...this.props} />
    }
}
</code>```

and replace both Views with PureView?

答:
Actually yes re-render will occur because re-render in react belongs to two things.

1. The props of the parent is changed.

2. The state of a component changed.

So in your case state of a component is changing that's why your sibling will also re-render because the state of the parent of these two siblings is been changed.

To avoid this, There are two doing this.

1. Use Pure component

Pure component looked at previous state and next state and handle your re-render smartly but the pure component is not applicable or useful in every case.

2. ShouldComponentUpdate

This method is used for handling re-renders manually like as in your case you don't want to re-render your second sibling so you can return false according to your prop in the lifecycle method of your sibling so this component will not re-render by this change.

The View is just a UI container re-rendering depends on component props or state, So View can't control the re-render of a component.

Body:
In React Native, say I have something like

var styles = { ... }
export default class App extends React.Component {
    ...
    render() {
        return(
            <View style={styles.container}>
                <View style={{width: this.state.widthA}} />
                <View style={{width: this.state.widthB}} />
            </View>
        );
    }
}

when the state widthA changes, the render() function is called again. Will the sibling View (widthB) also re-render? Would there be any performance boost to define

class PureView extends React.PureComponent {
    constructor(props) {
        super(props);
    }
    render() {
        return <View {...this.props} />
    }
}

and replace both Views with PureView?


问:
OpenCV compiling issues
With compiling this project I have several issues:

Due to "file not found" I had to change header
from:

```<code>#include <cv.h>
#include <highgui.h>
</code>```

to:

```<code>#include <opencv.hpp>
#include <highgui.hpp>
</code>```

that solved Include issues but compiling give several more faults:

What I'm doing wrong?
Below whole code:

```<code>#include <stdio.h>
#include <unistd.h>
#include <opencv.hpp>
#include <highgui.hpp>

int main(int argc, char **argv)
{
    // get command line parameters
    if(argc < 6)
    {
        printf("Usage: %s <video> <vga window x> <vga window y> <vga width> <vga height>\n", argv[0]);
        printf("Usage: %s <camera id> <vga window x> <vga window y> <vga width> <vga height>\n", argv[0]);
        exit(0);
    }
    const char *filename = argv[1];
    int vgaX = atoi(argv[2]);
    int vgaY = atoi(argv[3]);
    int vgaWidth = atoi(argv[4]);
    int vgaHeight = atoi(argv[5]);
    int outputPixelCount = vgaWidth * vgaHeight;

    // opencv initializations
    CvCapture* cap = cvCaptureFromFile(filename);
    int isVideoFile = cap != 0;
    if (!cap)
        cap = cvCaptureFromCAM(atoi(filename));
    if (!cap)
    {
        printf("Could not open file/camera!\n");
        exit(1);
    }
    IplImage* frame = cvQueryFrame(cap); // get first frame for size
    if (!frame)
    {
        printf("The Video is empty!\n");
        cvReleaseCapture(&cap);
        exit(1);
    }
    IplImage* edges = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
    IplImage* lines = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
    IplImage* out = cvCreateImage(cvSize(vgaWidth, vgaHeight), IPL_DEPTH_8U, 3);
    int outStep = out->widthStep;
    int outChannels = out->nChannels;
    unsigned char *outData = (unsigned char*)out->imageData;

    // position windows // TODO: make debug output windows optional?
    cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("frame", 0, 32);
    cvNamedWindow("edges", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("edges", frame->width, 32);
    cvNamedWindow("lines", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("lines", 2 * frame->width, 32);
    cvNamedWindow("out", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("out", vgaX, vgaY);

    while (42)
    {
        frame = cvQueryFrame(cap);
        if (!frame || (cvWaitKey(1) & 0xff) == 'q')
            break;
        cvShowImage("frame", frame);

        // edge detection
        cvCanny(frame, edges, 128.0, 130.0, 3); // TODO: tweakable parameters?
        cvShowImage("edges", edges);

        // get contours
        CvMemStorage *storage = cvCreateMemStorage(0);
        CvSeq *contours;
        int contourCount = cvFindContours(
            edges, storage, &contours, sizeof(CvContour),
            CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
        cvZero(lines);
        cvDrawContours(lines, contours, cvScalar(32, 255, 32, 255), cvScalarAll(0), 100, 1, 8, cvPoint(0, 0));
        cvShowImage("lines", lines);

        // calculate total length over all contours
        float contourLengthSum = 0.0f;
        for(CvSeq *c = contours; c; c = c->h_next)
        {
            for(int i = 0; i < c->total - 1; i++)
            {
                CvPoint *p0 = CV_GET_SEQ_ELEM(CvPoint, c, i);
                CvPoint *p1 = CV_GET_SEQ_ELEM(CvPoint, c, i + 1);
                int dx = p1->x - p0->x;
                int dy = p1->y - p0->y;
                contourLengthSum += sqrtf(dx * dx + dy * dy);
            }
        }
        float factor = (float)outputPixelCount / contourLengthSum;

        // write output image
        int cx = 0, cy = 0;
        float xScale = 255.0f / frame->width;
        float yScale = 255.0f / frame->height;
        unsigned char *dp = &outData[1];
        for(CvSeq *c = contours; c; c = c->h_next)
        {
            CvPoint *p0 = CV_GET_SEQ_ELEM(CvPoint, c, 0);
            for(int i = 1; i < c->total; i++)
            {
                CvPoint *p1 = CV_GET_SEQ_ELEM(CvPoint, c, i);
                float x0x1 = p1->x - p0->x;
                float y0y1 = p1->y - p0->y;
                int n = (int)(sqrtf(x0x1 * x0x1 + y0y1 * y0y1) * factor);
                float x =          (float)p0->x * xScale;
                float y = 255.0f - (float)p0->y * yScale;
                float dt = 1.0f / (float)(n - 1);
                float dx = dt *  x0x1 * xScale;
                float dy = dt * -y0y1 * yScale;
                for (int j = 0; j < n; j++)
                {
                    dp[0] = (unsigned char)x;
                    dp[1] = (unsigned char)y;
                    x += dx;
                    y += dy;
                    dp += outChannels;
                    if (++cx == vgaWidth)
                    {
                        cx = 0;
                        dp = &outData[++cy * outStep + 1];
                        if (cy == vgaHeight)
                            goto full;
                    }
                }
                p0 = p1;
            }
        }

        // fill last few pixels with last pixel value, if there are any left
        for (; cy < vgaHeight; cy++)
        {
            for (;cx < vgaWidth; cx++)
            {
                outData[cy * outStep + cx * outChannels + 1] = 0;
                outData[cy * outStep + cx * outChannels + 2] = 0;
            }
            cx = 0;
        }

        full:
        cvReleaseMemStorage(&storage);
        cvShowImage("out", out);

        //if (isVideoFile)
        //  usleep(8000); // TODO: proper synchronization
    }
    cvReleaseImage(&out);
    cvReleaseImage(&lines);
    cvReleaseImage(&edges);
    cvReleaseCapture(&cap);
    return 0;
}
</code>```

答:
Looks like you are using old OpenCV API. They are replaced with new methods. Your code does not work with recent OpenCV 4.3.0. cvCaptureFromFile, cvCaptureFromCAM and some others exists in 4.0.0-rc of OpenCV docs.

https://docs.opencv.org/4.0.0-rc/dd/d01/group__videoio__c.html

After 4.0.0-rc of OpenCV the documentation points to videoio.

https://docs.opencv.org/master/dd/de7/group__videoio.html

Further Reference:

https://answers.opencv.org/question/55344/undeclared-indentifier-opencv-cvcapturefromcam-and-cvqueryframe/

Also if you have include problems in new OpenCV version try,

```<code>#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
</code>```

Title: OpenCV compiling issues

Body:
With compiling this project I have several issues:

Due to "file not found" I had to change header
from:

#include <cv.h>
#include <highgui.h>

to:

#include <opencv.hpp>
#include <highgui.hpp>

that solved Include issues but compiling give several more faults:

What I'm doing wrong?
Below whole code:

#include <stdio.h>
#include <unistd.h>
#include <opencv.hpp>
#include <highgui.hpp>

int main(int argc, char **argv)
{
    // get command line parameters
    if(argc < 6)
    {
        printf("Usage: %s <video> <vga window x> <vga window y> <vga width> <vga height>\n", argv[0]);
        printf("Usage: %s <camera id> <vga window x> <vga window y> <vga width> <vga height>\n", argv[0]);
        exit(0);
    }
    const char *filename = argv[1];
    int vgaX = atoi(argv[2]);
    int vgaY = atoi(argv[3]);
    int vgaWidth = atoi(argv[4]);
    int vgaHeight = atoi(argv[5]);
    int outputPixelCount = vgaWidth * vgaHeight;

    // opencv initializations
    CvCapture* cap = cvCaptureFromFile(filename);
    int isVideoFile = cap != 0;
    if (!cap)
        cap = cvCaptureFromCAM(atoi(filename));
    if (!cap)
    {
        printf("Could not open file/camera!\n");
        exit(1);
    }
    IplImage* frame = cvQueryFrame(cap); // get first frame for size
    if (!frame)
    {
        printf("The Video is empty!\n");
        cvReleaseCapture(&cap);
        exit(1);
    }
    IplImage* edges = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
    IplImage* lines = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
    IplImage* out = cvCreateImage(cvSize(vgaWidth, vgaHeight), IPL_DEPTH_8U, 3);
    int outStep = out->widthStep;
    int outChannels = out->nChannels;
    unsigned char *outData = (unsigned char*)out->imageData;

    // position windows // TODO: make debug output windows optional?
    cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("frame", 0, 32);
    cvNamedWindow("edges", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("edges", frame->width, 32);
    cvNamedWindow("lines", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("lines", 2 * frame->width, 32);
    cvNamedWindow("out", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("out", vgaX, vgaY);

    while (42)
    {
        frame = cvQueryFrame(cap);
        if (!frame || (cvWaitKey(1) & 0xff) == 'q')
            break;
        cvShowImage("frame", frame);

        // edge detection
        cvCanny(frame, edges, 128.0, 130.0, 3); // TODO: tweakable parameters?
        cvShowImage("edges", edges);

        // get contours
        CvMemStorage *storage = cvCreateMemStorage(0);
        CvSeq *contours;
        int contourCount = cvFindContours(
            edges, storage, &contours, sizeof(CvContour),
            CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
        cvZero(lines);
        cvDrawContours(lines, contours, cvScalar(32, 255, 32, 255), cvScalarAll(0), 100, 1, 8, cvPoint(0, 0));
        cvShowImage("lines", lines);

        // calculate total length over all contours
        float contourLengthSum = 0.0f;
        for(CvSeq *c = contours; c; c = c->h_next)
        {
            for(int i = 0; i < c->total - 1; i++)
            {
                CvPoint *p0 = CV_GET_SEQ_ELEM(CvPoint, c, i);
                CvPoint *p1 = CV_GET_SEQ_ELEM(CvPoint, c, i + 1);
                int dx = p1->x - p0->x;
                int dy = p1->y - p0->y;
                contourLengthSum += sqrtf(dx * dx + dy * dy);
            }
        }
        float factor = (float)outputPixelCount / contourLengthSum;

        // write output image
        int cx = 0, cy = 0;
        float xScale = 255.0f / frame->width;
        float yScale = 255.0f / frame->height;
        unsigned char *dp = &outData[1];
        for(CvSeq *c = contours; c; c = c->h_next)
        {
            CvPoint *p0 = CV_GET_SEQ_ELEM(CvPoint, c, 0);
            for(int i = 1; i < c->total; i++)
            {
                CvPoint *p1 = CV_GET_SEQ_ELEM(CvPoint, c, i);
                float x0x1 = p1->x - p0->x;
                float y0y1 = p1->y - p0->y;
                int n = (int)(sqrtf(x0x1 * x0x1 + y0y1 * y0y1) * factor);
                float x =          (float)p0->x * xScale;
                float y = 255.0f - (float)p0->y * yScale;
                float dt = 1.0f / (float)(n - 1);
                float dx = dt *  x0x1 * xScale;
                float dy = dt * -y0y1 * yScale;
                for (int j = 0; j < n; j++)
                {
                    dp[0] = (unsigned char)x;
                    dp[1] = (unsigned char)y;
                    x += dx;
                    y += dy;
                    dp += outChannels;
                    if (++cx == vgaWidth)
                    {
                        cx = 0;
                        dp = &outData[++cy * outStep + 1];
                        if (cy == vgaHeight)
                            goto full;
                    }
                }
                p0 = p1;
            }
        }

        // fill last few pixels with last pixel value, if there are any left
        for (; cy < vgaHeight; cy++)
        {
            for (;cx < vgaWidth; cx++)
            {
                outData[cy * outStep + cx * outChannels + 1] = 0;
                outData[cy * outStep + cx * outChannels + 2] = 0;
            }
            cx = 0;
        }

        full:
        cvReleaseMemStorage(&storage);
        cvShowImage("out", out);

        //if (isVideoFile)
        //  usleep(8000); // TODO: proper synchronization
    }
    cvReleaseImage(&out);
    cvReleaseImage(&lines);
    cvReleaseImage(&edges);
    cvReleaseCapture(&cap);
    return 0;
}

Body:
Looks like you are using old OpenCV API. They are replaced with new methods. Your code does not work with recent OpenCV 4.3.0. cvCaptureFromFile, cvCaptureFromCAM and some others exists in 4.0.0-rc of OpenCV docs.

https://docs.opencv.org/4.0.0-rc/dd/d01/group__videoio__c.html

After 4.0.0-rc of OpenCV the documentation points to videoio.

https://docs.opencv.org/master/dd/de7/group__videoio.html

Further Reference:

https://answers.opencv.org/question/55344/undeclared-indentifier-opencv-cvcapturefromcam-and-cvqueryframe/

Also if you have include problems in new OpenCV version try,

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>


问:
Split numbers delimited by any non numeric string
I need to split a string with numbers separated by any non numeric string.
i.e.

```<code>"1111/222**3333333///44/ 55555"
</code>```

I need to get the string array:

```<code>[0] 1111
[1] 222
[2] 3333333
[3] 44
[4] 55555
</code>```

I have found a lot of regex examples, but no one is the right one.
Thanks.

答:
Regex.Split(String, String) splits an input string into an array of substrings at the positions defined by a regular expression pattern.

In your case, the pattern for any group of one-or-more non-digit characters would be @"\D+", so your code would be like this:

```<code>const string pattern = @"\D+";
var input = "1111/222**3333333///44/ 55555";
var output = Regex.Split(input, pattern);
</code>```

Body:
I need to split a string with numbers separated by any non numeric string.
i.e.

"1111/222**3333333///44/ 55555"

I need to get the string array:

[0] 1111
[1] 222
[2] 3333333
[3] 44
[4] 55555

I have found a lot of regex examples, but no one is the right one.
Thanks.

Body:
Regex.Split(String, String) splits an input string into an array of substrings at the positions defined by a regular expression pattern.

In your case, the pattern for any group of one-or-more non-digit characters would be @"\D+", so your code would be like this:

const string pattern = @"\D+";
var input = "1111/222**3333333///44/ 55555";
var output = Regex.Split(input, pattern);


问:
How to check if a UIButtons title is equal to a specific string?
I am trying to have a button that you can click and it will do x if the title is that string, other wise no.</p>

```<code>if button.setTitle("Message", for: .normal) == true {

} else {

}
</code>```

答:
You can use the currentTitle property to check the value you want.

The setTitle method should be used to set/update the title value, not get it.

Body:
I am trying to have a button that you can click and it will do x if the title is that string, other wise no.

if button.setTitle("Message", for: .normal) == true {

} else {

}


问:
Access violation exception in 64bit MFC version
I am trying to port a 32bit application to 64 bit. The calculations appear to run correctly, but I cannot configure the views properly. I am using MFC, C++ and OpenGL, Intel 10.0.027 compiler inside VS2005 on a W7 x64 machine.

When the crash happens, I get the following message: "First-chance exception at 0xffffffff8043b1b6 in 3DApp.exe: 0xC0000005: Access violation at location 0xffffffff8043b1b6" and this is the stack trace:

```<code>ffffffff8043b1b6()
user32.dll!UserCallWinProcCheckWow()  + 0x11d bytes
user32.dll!DispatchMessageWorker()  + 0x12a bytes
3DApp.exe!AfxInternalPumpMessage()  Line 183    C++
3DApp.exe!CWinThread::PumpMessage()  Line 896   C++
3DApp.exe!CWinThread::Run()  Line 625 + 0x13 bytes  C++
</code>```

NOTE PLEASE why is UserCallWinProcCheckWow called, I thought Wow suffix was only for emulating 32 bit applications on 64 bit computer

Obviously, somewhere a 64 bit pointer gets treated as a 32 bit pointer, but I cannot pin point where that happens. I loaded debug symbols from microsoft, which show the top of the call stack.

Any help greatly appreciated.
Leon

EDIT

The code calling DispatchMessage is:

```<code>    if (pState->m_msgCur.message != WM_KICKIDLE && !AfxPreTranslateMessage(&(pState->m_msgCur)))
{
    ::TranslateMessage(&(pState->m_msgCur));
    ::DispatchMessage(&(pState->m_msgCur));
}
</code>```

The pointers of pState appear to be 64 bit at this stage.

State of pState->m_msgCu:

pState->m_msgCur    {msg=0x00000022 wp=0x0000000000000000 lp=0x0000000000000000}    tagMSG

hwnd    0x0000000000020416 {unused=0x00000000 } HWND__ *
    message 0x00000022  unsigned int
    wParam  0x0000000000000000  unsigned __int64
    lParam  0x0000000000000000  __int64
    time    0x000f3967  unsigned long

pt  {x=0x0000030f y=0x00000356} tagPOINT

答: I have just fixed an issue with the same behavior. The bug was in passing parameters to SetWindowLongPtr( GWLP_WNDPROC ), pointer to WndProc was improperly casted to LONG instead of correct LONG_PTR. This way, bad pointer to WndProc was put into internal OS WND struct, so any following call to the HWND's WndProc crashed.</p>

ContentLicense: CC BY-SA 2.5

Body:
I am trying to port a 32bit application to 64 bit. The calculations appear to run correctly, but I cannot configure the views properly. I am using MFC, C++ and OpenGL, Intel 10.0.027 compiler inside VS2005 on a W7 x64 machine.

When the crash happens, I get the following message: "First-chance exception at 0xffffffff8043b1b6 in 3DApp.exe: 0xC0000005: Access violation at location 0xffffffff8043b1b6" and this is the stack trace:

ffffffff8043b1b6()
user32.dll!UserCallWinProcCheckWow()  + 0x11d bytes
user32.dll!DispatchMessageWorker()  + 0x12a bytes
3DApp.exe!AfxInternalPumpMessage()  Line 183    C++
3DApp.exe!CWinThread::PumpMessage()  Line 896   C++
3DApp.exe!CWinThread::Run()  Line 625 + 0x13 bytes  C++

NOTE PLEASE why is UserCallWinProcCheckWow called, I thought Wow suffix was only for emulating 32 bit applications on 64 bit computer

Obviously, somewhere a 64 bit pointer gets treated as a 32 bit pointer, but I cannot pin point where that happens. I loaded debug symbols from microsoft, which show the top of the call stack.

Any help greatly appreciated.
Leon

EDIT

The code calling DispatchMessage is:

    if (pState->m_msgCur.message != WM_KICKIDLE && !AfxPreTranslateMessage(&(pState->m_msgCur)))
{
    ::TranslateMessage(&(pState->m_msgCur));
    ::DispatchMessage(&(pState->m_msgCur));
}

The pointers of pState appear to be 64 bit at this stage.

State of pState->m_msgCu:

pState->m_msgCur    {msg=0x00000022 wp=0x0000000000000000 lp=0x0000000000000000}    tagMSG

hwnd    0x0000000000020416 {unused=0x00000000 } HWND__ *
    message 0x00000022  unsigned int
    wParam  0x0000000000000000  unsigned __int64
    lParam  0x0000000000000000  __int64
    time    0x000f3967  unsigned long

pt  {x=0x0000030f y=0x00000356} tagPOINT


问:
How is spark task with extreme long line processed?
I was using pyspark to process multiple log files, in which a record is split into multiple-line format, so I chose wholeTextFiles to read the data and then filter out what I want. Size of each file is ~800M and there are 4096 files in total. However, spark job crashed after processing some tasks, the following is my configuration and code.

configuration:

--num-executors 100
--executor-cores    1
--executor-memory   30G

core code:

```<code>file_rdd= sc.wholeTextFiles(inputDir, 2500)
print file_rdd.getNumPartitions()
out_rdd = file_rdd.flatMap(parseFileContent)\
                  .repartition(1000)\
                  .saveAsTextFile(title_outputDir)
</code>```

It went crash after 300+ tasks were complete, running log showed as follows:

I also get the DAG Visualization here:

it seems like repartition operation causes the crash, and stage 1 never starts.
BTW, here is more info about the tasks processed if it helps:

I find some related questions on so here, here and here, but they only explain high-level memory consumption rather than how the tasks are executed (with respect to each line in a file) and how many memory is needed for each line (when it is processed and after it is processed). Hope someone could help me out of this complicated notion, and any suggestions would be appreciated about how to process multi-line format file in pyspark (not scala, I know scala could use DataFrame to overcome this problem but I am not familiar with scala), thank you all!

答: .repartition(1000) will balance your spark partitions and is an expensive operation, .coalesce(1000) will just stack partitions on top of each other and not work too hard to balance them and is thus much less expensive operation.  Use .coalesce() to decrease the number of partitions from 4096 to 1000, and save .repartition() for when you need to increase the number of spark partitions. Try .coaleesce(1000), you will end up with 1000 files in your output.

Body:
I was using pyspark to process multiple log files, in which a record is split into multiple-line format, so I chose wholeTextFiles to read the data and then filter out what I want. Size of each file is ~800M and there are 4096 files in total. However, spark job crashed after processing some tasks, the following is my configuration and code.

configuration:

--num-executors 100
--executor-cores    1
--executor-memory   30G

core code:

file_rdd= sc.wholeTextFiles(inputDir, 2500)
print file_rdd.getNumPartitions()
out_rdd = file_rdd.flatMap(parseFileContent)\
                  .repartition(1000)\
                  .saveAsTextFile(title_outputDir)

It went crash after 300+ tasks were complete, running log showed as follows:

I also get the DAG Visualization here:

it seems like repartition operation causes the crash, and stage 1 never starts.
BTW, here is more info about the tasks processed if it helps:

I find some related questions on so here, here and here, but they only explain high-level memory consumption rather than how the tasks are executed (with respect to each line in a file) and how many memory is needed for each line (when it is processed and after it is processed). Hope someone could help me out of this complicated notion, and any suggestions would be appreciated about how to process multi-line format file in pyspark (not scala, I know scala could use DataFrame to overcome this problem but I am not familiar with scala), thank you all!


问:
Using operator (or synthetic function) as a parameter
Below code works fine

```<code>  def exec(f: (Int, Int) => Boolean, p1: Int, p2: Int) = f(p1, p2)
  val >= = (x1: Int, x2: Int) => x1 >= x2
  println(exec(>=, 10, 10))
</code>```

however the question is, if it's possible to get it working without explicit re-defining operator (synthetic function)?

Update

It's clear that it works absolutely fine like this

```<code>println(exec(_ >= _, 10, 10))
</code>```

The question is whether it's possible to make it working in a form exec(>=, 10, 10) without defining functional value.

答:
Complementing my other answer you may do this:</p>

```<code>println(exec(10, 10)(_ >= _))
</code>```

Body:
Below code works fine

  def exec(f: (Int, Int) => Boolean, p1: Int, p2: Int) = f(p1, p2)
  val >= = (x1: Int, x2: Int) => x1 >= x2
  println(exec(>=, 10, 10))

however the question is, if it's possible to get it working without explicit re-defining operator (synthetic function)?

Update

It's clear that it works absolutely fine like this

println(exec(_ >= _, 10, 10))

The question is whether it's possible to make it working in a form exec(>=, 10, 10) without defining functional value.

Body:
Complementing my other answer you may do this:

println(exec(10, 10)(_ >= _))